的想法:--我想用HTTP身份验证保护Spring的每个站点,但希望通过错误页面将其重定向到/welcome (当然,用户必须经过身份验证,否则他会看到基本的身份验证对话框)。
问题:每当我尝试访问站点时,都会弹出基本身份验证对话框。但是当我取消对话框时,也就是按cancel键,我会在受保护的欢迎页面上看到所有重要的/安全的信息--没有基本的身份验证对话框!
样本控制器:
@Controller
public class WelcomeController
{
// Welcome is a protected site!
@RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
public ModelAndView welcome()
{
return new ModelAndView("welcome");
}
}安全配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity security) throws Exception
{
// Set the security settings
security.httpBasic().and().authorizeRequests().anyRequest().authenticated().and().csrf();
}
}web.xml:
<?xml version="1.0" encoding="UTF-8"?>
... Snipped ...
<servlet-mapping>
<servlet-name>testapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>400</error-code>
<location>/welcome</location>
</error-page>
<error-page>
<error-code>401</error-code>
<location>/welcome</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/welcome</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/welcome</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/welcome</location>
</error-page>
</web-app>发布于 2015-12-17 15:40:23
我还必须为客户实现相同的设置。此外,我们在应用服务器前面有一个web应用程序防火墙。
我仍然不知道Tomcat如何忽略Spring的身份验证检查。重要的是不要乱搞401、402和403个HTTP代码(我从web.xml中删除了它们的处理程序)
我最终得到了一个通用错误页面。如果你自己处理500个错误,这是可以讨论的。如果您的系统抛出500个错误,您可能无法处理它,因为您的系统刚刚抛出了500个错误,并且在错误处理过程中会遇到另500个错误->使用简单的模型和视图、静态HTML站点或重定向到错误处理程序servlet。
春季保安:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity security) throws Exception
{
// Set the security settings
security.httpBasic().and().authorizeRequests().anyRequest().authenticated().and().csrf();
}
}web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<!-- Snipped -->
<error-page>
<error-code>404</error-code>
<location>/error</location>
</error-page>
</web-app>404错误操作:
@Controller
public class ErrorController
{
@RequestMapping(value = "/error", method = RequestMethod.GET)
public ModelAndView addresses()
{
return new ModelAndView("error"); // Or redirect to /welcome
}
}https://stackoverflow.com/questions/34212591
复制相似问题