南充北京网站建设域名价格排行
web/
2025/9/30 9:46:08/
文章来源:
南充北京网站建设,域名价格排行,网站空间租,广州推广系统目录
一、EnableWebMvcSecurity
二、MvcRequestMatcher
三、AuthenticationPrincipal
四、异步 Spring MVC 整合
五、Spring MVC 和 CSRF 整合
1、自动包含 Token
2、解析 CsrfToken Spring Security提供了一些与Spring MVC的可选整合。本节将进一步详细介绍这种整合。 …目录
一、EnableWebMvcSecurity
二、MvcRequestMatcher
三、AuthenticationPrincipal
四、异步 Spring MVC 整合
五、Spring MVC 和 CSRF 整合
1、自动包含 Token
2、解析 CsrfToken Spring Security提供了一些与Spring MVC的可选整合。本节将进一步详细介绍这种整合。
一、EnableWebMvcSecurity 从Spring Security 4.0开始EnableWebMvcSecurity 已被弃用。取而代之的是 EnableWebSecurity它根据classpath增加了Spring MVC功能。
要启用Spring Security与Spring MVC的整合请在配置中添加 EnableWebSecurity 注解。 Spring Security通过使用Spring MVC的 WebMvcConfigurer 提供配置。这意味着如果你使用更高级的选项如直接与 WebMvcConfigurationSupport 集成你需要手动提供Spring Security的配置。
二、MvcRequestMatcher
Spring Security提供了与Spring MVC如何通过 MvcRequestMatcher 匹配URL的深度集成。这有助于确保你的安全规则与用于处理请求的逻辑相匹配。
要使用 MvcRequestMatcher你必须将Spring Security配置放在与 DispatcherServlet 相同的 ApplicationContext 中。这是必要的因为Spring Security的 MvcRequestMatcher 期望一个名称为 mvcHandlerMappingIntrospector 的 HandlerMappingIntrospector Bean被你的Spring MVC配置注册用于执行匹配。
对于 web.xml 文件这意味着你应该把你的配置放在 DispatcherServlet.xml 中。
listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class
/listener!-- All Spring Configuration (both MVC and Security) are in /WEB-INF/spring/ --
context-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/spring/*.xml/param-value
/context-paramservletservlet-namespring/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class!-- Load from the ContextLoaderListener --init-paramparam-namecontextConfigLocation/param-nameparam-value/param-value/init-param
/servletservlet-mappingservlet-namespring/servlet-nameurl-pattern//url-pattern
/servlet-mapping
以下 WebSecurityConfiguration 被放置在 DispatcherServlet 的 ApplicationContext 中。
Java
public class SecurityInitializer extendsAbstractAnnotationConfigDispatcherServletInitializer {Overrideprotected Class?[] getRootConfigClasses() {return null;}Overrideprotected Class?[] getServletConfigClasses() {return new Class[] { RootConfiguration.class,WebMvcConfiguration.class };}Overrideprotected String[] getServletMappings() {return new String[] { / };}
} 我们总是建议你通过对 HttpServletRequest 和安全方法method security进行匹配来提供授权规则。 通过对 HttpServletRequest 的匹配来提供授权规则是很好的因为它发生在代码路径的早期有助于减少 attack surface攻击面。安全方法确保如果有人绕过了网络授权规则你的应用程序仍然是安全的。这就是所谓的 Defense in Depth深度防御
考虑一个Controller它有如下mapping。
Java
RequestMapping(/admin)
public String admin() {// ...
}
为了限制管理员用户对这个Controller方法的访问你可以通过在 HttpServletRequest 上匹配以下内容来提供授权规则。
Java
Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http.authorizeHttpRequests((authorize) - authorize.requestMatchers(/admin).hasRole(ADMIN));return http.build();
}
在xml中实现相同功能。
httpintercept-url pattern/admin accesshasRole(ADMIN)/
/http
无论是哪种配置/admin URL都要求被认证的用户是一个管理员用户。然而根据我们的Spring MVC配置/admin.html URL也映射到我们的 admin() 方法。此外根据我们的Spring MVC配置/admin URL也映射到我们的 admin() 方法。
问题是我们的安全规则只保护 /admin。我们可以为Spring MVC的所有排列组合添加额外的规则但这将是相当冗长和乏味的。
幸运的是当使用 requestMatchers DSL方法时如果Spring Security检测到Spring MVC在classpath中可用它会自动创建一个 MvcRequestMatcher。因此它将通过使用Spring MVC对URL进行匹配来保护Spring MVC的相同URL。
在使用 Spring MVC 时一个常见的要求是指定servlet路径属性为此你可以使用 MvcRequestMatcher.Builder 来创建多个共享相同servlet路径的 MvcRequestMatcher 实例。
Java
Bean
public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {MvcRequestMatcher.Builder mvcMatcherBuilder new MvcRequestMatcher.Builder(introspector).servletPath(/path);http.authorizeHttpRequests((authorize) - authorize.requestMatchers(mvcMatcherBuilder.pattern(/admin)).hasRole(ADMIN).requestMatchers(mvcMatcherBuilder.pattern(/user)).hasRole(USER));return http.build();
}
下面的XML具有相同的效果。
http request-matchermvcintercept-url pattern/admin accesshasRole(ADMIN)/
/http
三、AuthenticationPrincipal
Spring Security提供了 AuthenticationPrincipalArgumentResolver它可以自动解析Spring MVC参数的当前 Authentication.getPrincipal()。通过使用 EnableWebSecurity你会自动将其添加到你的Spring MVC配置中。如果你使用基于XML的配置你必须自己添加这个。
mvc:annotation-drivenmvc:argument-resolversbean classorg.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver //mvc:argument-resolvers
/mvc:annotation-driven
一旦你正确配置了 AuthenticationPrincipalArgumentResolver你就可以在Spring MVC层中完全与Spring Security脱钩。
考虑这样一种情况一个自定义的 UserDetailsService 返回一个实现 UserDetails 的 Object 和你自己的 CustomUser Object。当前认证的用户的 CustomUser 可以通过使用以下代码来访问。
JavaKotlin
RequestMapping(/messages/inbox)
public ModelAndView findMessagesForUser() {Authentication authentication SecurityContextHolder.getContext().getAuthentication();CustomUser custom (CustomUser) authentication null ? null : authentication.getPrincipal();// .. find messages for this user and return them ...
}
从 Spring Security 3.2 开始我们可以通过添加一个注解来更直接地解决这个争论。
Java
import org.springframework.security.core.annotation.AuthenticationPrincipal;// ...RequestMapping(/messages/inbox)
public ModelAndView findMessagesForUser(AuthenticationPrincipal CustomUser customUser) {// .. find messages for this user and return them ...
}
有时你可能需要以某种方式转变 principal。例如如果 CustomUser 需要是 final 的它就不能被继承。在这种情况下UserDetailsService 可能会返回一个实现 UserDetails 的 Object并提供一个名为 getCustomUser 的方法来访问 CustomUser。
Java
public class CustomUserUserDetails extends User {// ...public CustomUser getCustomUser() {return customUser;}
}
然后我们可以通过使用 SpEL 表达式 访问 CustomUser该表达式使用 Authentication.getPrincipal() 作为根对象。
Java
import org.springframework.security.core.annotation.AuthenticationPrincipal;// ...RequestMapping(/messages/inbox)
public ModelAndView findMessagesForUser(AuthenticationPrincipal(expression customUser) CustomUser customUser) {// .. find messages for this user and return them ...
}
我们也可以在我们的SpEL表达式中引用Bean。例如如果我们使用JPA来管理我们的用户如果我们想修改和保存当前用户的一个属性我们可以使用下面的方法。
Java
import org.springframework.security.core.annotation.AuthenticationPrincipal;// ...PutMapping(/users/self)
public ModelAndView updateName(AuthenticationPrincipal(expression jpaEntityManager.merge(#this)) CustomUser attachedCustomUser,RequestParam String firstName) {// change the firstName on an attached instance which will be persisted to the databaseattachedCustomUser.setFirstName(firstName);// ...
}
我们可以通过让 AuthenticationPrincipal 成为我们自己注解的元注解来进一步消除对 Spring Security 的依赖。下一个例子演示了我们如何在一个名为 CurrentUser 的注解上这样做。 为了消除对Spring Security的依赖消费应用程序将创建 CurrentUser。这一步不是严格要求的但有助于将你对Spring Security的依赖性隔离到一个更集中的位置。
Java
Target({ElementType.PARAMETER, ElementType.TYPE})
Retention(RetentionPolicy.RUNTIME)
Documented
AuthenticationPrincipal
public interface CurrentUser {}
我们已经将对Spring Security的依赖性隔离到一个文件中。现在 CurrentUser 已经被指定我们可以用它来发出信号signal来解决我们的当前认证用户的 CustomUser。
Java
RequestMapping(/messages/inbox)
public ModelAndView findMessagesForUser(CurrentUser CustomUser customUser) {// .. find messages for this user and return them ...
}
四、异步 Spring MVC 整合
Spring Web MVC 3.2对 异步请求处理有很好的支持。不需要额外的配置Spring Security会自动将 SecurityContext 设置为调用 Controller 返回的 Callable 的 Thread。例如下面的方法会自动用创建 Callable 时可用的 SecurityContext 来调用它的 Callable。
Java
RequestMapping(methodRequestMethod.POST)
public CallableString processUpload(final MultipartFile file) {return new CallableString() {public Object call() throws Exception {// ...return someView;}
};
} Associating SecurityContext to Callable’s 将 SecurityContext 与 Callable 关联起来从技术上讲Spring Security与 WebAsyncManager 集成。用于处理 Callable 的 SecurityContext 是 startCallableProcessing 被调用时存在于 SecurityContextHolder 上的 SecurityContext。
没有与 Controller 返回的 DeferredResult 的自动整合。这是因为 DeferredResult 是由用户处理的因此没有办法与它自动整合。然而你仍然可以使用 并发支持来提供与Spring Security的透明整合。
五、Spring MVC 和 CSRF 整合
Spring Security与Spring MVC集成增加CSRF保护。
1、自动包含 Token
Spring Security 在使用 Spring MVC表单标签 的表单中自动 [包含CSRF Token。考虑一下下面的JSP。
jsp:root xmlns:jsphttp://java.sun.com/JSP/Pagexmlns:chttp://java.sun.com/jsp/jstl/corexmlns:formhttp://www.springframework.org/tags/form version2.0jsp:directive.page languagejava contentTypetext/html /
html xmlnshttp://www.w3.org/1999/xhtml langen xml:langen!-- ... --c:url varlogoutUrl value/logout/form:form action${logoutUrl}methodpostinput typesubmitvalueLog out /input typehiddenname${_csrf.parameterName}value${_csrf.token}//form:form!-- ... --
/html
/jsp:root
前面的例子输出的HTML与下面类似。
!-- ... --form action/context/logout methodpost
input typesubmit valueLog out/
input typehidden name_csrf valuef81d4fae-7dec-11d0-a765-00a0c91e6bf6/
/form!-- ... --
2、解析 CsrfToken
Spring Security提供了 CsrfTokenArgumentResolver它可以自动解析Spring MVC参数的当前 CsrfToken。通过使用 EnableWebSecurity你会自动将其添加到你的Spring MVC配置中。如果你使用基于XML的配置你必须自己添加这个。
一旦 CsrfTokenArgumentResolver 被正确配置你就可以将 CsrfToken 暴露给你基于HTML的静态应用程序。
Java
RestController
public class CsrfController {RequestMapping(/csrf)public CsrfToken csrf(CsrfToken token) {return token;}
}
对其他域保持 CsrfToken 的 secret 是很重要的。这意味着如果你使用 跨源资源共享CORS你不应该将 CsrfToken 暴露给任何外部域。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/84388.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!