序言:从Spring-Security 3.2开始
@AuthenticationPrincipal,此答案的末尾描述了一个不错的注释。当你使用Spring-Security> = 3.2时,这是最好的方法。
当你:
- 使用较旧版本的Spring-Security,
- 需要通过存储在主体中的某些信息(例如登录名或ID)从数据库加载你的自定义用户对象,
- 想要了解
a HandlerMethodArgumentResolver
或如何WebArgumentResolver
以一种优雅的方式解决此问题,或者只是想要了解@AuthenticationPrincipaland
的背景AuthenticationPrincipalArgumentResolver
(因为它基于HandlerMethodArgumentResolver)
(顺便说一句:我的回答有点老(2012年1月),因此是Lukas Schmelzeisen作为第一个使用@AuthenticationPrincipal基于Spring Security 3.2 的注释解决方案的人。)
然后你可以在控制器中使用
public ModelAndView someRequestHandler(Principal principal) { User activeUser = (User) ((Authentication) principal).getPrincipal(); ...}如果你需要一次,那没关系。但是,如果你因其丑陋的原因而需要它的几倍,因为它会污染你的控制器的基础结构详细信息,则通常应将其隐藏在框架中。
因此,你可能真正想要的是拥有这样的控制器:
public ModelAndView someRequestHandler(@ActiveUser User activeUser) { ...}因此,你只需要实现即可WebArgumentResolver。它有一种方法
Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception
这将获取Web请求(第二个参数),并且User如果感觉对方法参数(第一个参数)负责,则必须返回。
从Spring 3.1开始,有一个新概念称为HandlerMethodArgumentResolver。如果使用Spring 3.1+,则应该使用它。(此答案的下一部分对此进行了说明)
public class CurrentUserWebArgumentResolver implements WebArgumentResolver{ Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) { if(methodParameter is for type User && methodParameter is annotated with @ActiveUser) {Principal principal = webRequest.getUserPrincipal();return (User) ((Authentication) principal).getPrincipal(); } else {return WebArgumentResolver.UNRESOLVED; } }}你需要定义自定义注释-如果应始终从安全上下文中获取User的每个实例,但绝不要将其作为命令对象,则可以跳过它。
@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)@documentedpublic @interface ActiveUser {}在配置中,你只需要添加以下内容:
<bean id="applicationConversionService"> <property name="customArgumentResolver"> <bean /> </property></bean>
应该注意的是,如果你使用的是Spring 3.1,他们建议使用HandlerMethodArgumentResolver而不是WebArgumentResolver。-见周杰伦的评论
与HandlerMethodArgumentResolverSpring 3.1+ 相同
public class CurrentUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter methodParameter) { return methodParameter.getParameterAnnotation(ActiveUser.class) != null && methodParameter.getParameterType().equals(User.class); } @Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { if (this.supportsParameter(methodParameter)) { Principal principal = webRequest.getUserPrincipal(); return (User) ((Authentication) principal).getPrincipal(); } else { return WebArgumentResolver.UNRESOLVED; } }}在配置中,你需要添加此
<mvc:annotation-driven> <mvc:argument-resolvers><bean /> </mvc:argument-resolvers> </mvc:annotation-driven>
Spring-Security 3.2解决方案
spring安全3.2(不使用Spring 3.2混淆)在溶液中有自己的编译:
@AuthenticationPrincipal(org.springframework.security.web.bind.annotation.AuthenticationPrincipal)。Lukas Schmelzeisen的回答对此进行了很好的描述。
只是在写
ModelAndView someRequestHandler(@AuthenticationPrincipal User activeUser) { ... }要使此工作正常进行,你需要注册
AuthenticationPrincipalArgumentResolver(org.springframework.security.web.bind.support.AuthenticationPrincipalArgumentResolver):通过“激活”
@EnableWebMvcSecurity或在其中注册此
Bean,mvc:argument-resolvers这与我在上面的Spring 3.1解决方案中描述的方式相同。
@请参阅《Spring Security 3.2参考》的第11.2章。@AuthenticationPrincipal
Spring-Security 4.0解决方案
它的工作方式类似于Spring 3.2解决方案,但在Spring 4.0中,@AuthenticationPrincipal和AuthenticationPrincipalArgumentResolver被“移动”到另一个软件包中:
- org.springframework.security.core.annotation.AuthenticationPrincipal
- org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver
(但是旧包中的旧类仍然存在,因此请不要混合使用!)
只是在写
import org.springframework.security.core.annotation.AuthenticationPrincipal;ModelAndView someRequestHandler(@AuthenticationPrincipal User activeUser) { ...}要使此工作正常进行,你需要注册
(org.springframework.security.web.method.annotation.)AuthenticationPrincipalArgumentResolver:通过“激活”
@EnableWebMvcSecurity或在其中注册此Bean,
mvc:argument-resolvers这与我在上面的Spring 3.1解决方案中描述的方式相同。
<mvc:annotation-driven> <mvc:argument-resolvers> <bean /> </mvc:argument-resolvers></mvc:annotation-driven>



