gpt4 book ai didi

java - 使用 web.xml 中的 Spring 安全性和错误页面保护 Spring Web MVC 应用程序

转载 作者:行者123 更新时间:2023-12-01 10:45:29 26 4
gpt4 key购买 nike

想法:我想通过 HTTP 基本身份验证保护 Spring Web MVC 的每个站点,但希望在出现 40* 或 500 服务器错误时通过错误页面重定向到/welcome (当然,用户必须经过身份验证,否则他将看到基本身份验证对话框)。

问题:每次我尝试访问该站点时,基本身份验证对话框都会按预期弹出。但是,当我取消对话框(即按取消)时,我会进入 protected 欢迎页面 [并查看所有重要/安全信息] - 没有基本身份验证对话框!

示例 Controller :

@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>

最佳答案

我还必须为客户实现相同的设置。此外,我们在应用程序服务器前面有一个 Web 应用程序防火墙。

我还是不知道Tomcat是怎么忽略Spring的认证检查的。重要的是不要乱用 401、402 和 403 HTTP 代码(我从 web.xml 中删除了它们的处理程序)

我最终得到了一个通用错误页面。如果你应该自己处理 500 个错误,这是可以讨论的。如果你的系统抛出了 500 错误,你可能无法处理它,因为你的系统刚刚抛出了 500 错误,并且在错误处理过程中你将遇到另一个 500 错误 --> 使用简单的模型和 View 、静态 HTML 站点或重定向到错误处理程序 servlet。

Spring 安全:

@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
}
}

关于java - 使用 web.xml 中的 Spring 安全性和错误页面保护 Spring Web MVC 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34212591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com