具有角色的经过身份validation的用户的Spring Security Java配置

我试图使用Spring Boot使用Java配置来配置Spring Security。

我的WebSecurityConfig类有一个这样的配置方法

@Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/", "/login", "/css/**", "/js/**", "/img/**", "/bootstrap/**" ).permitAll() .antMatchers("/individual", "/application", "/upload").hasRole("USER") .and() .formLogin() .loginPage("/login") .successHandler(successHandler); } 

我为未经身份validation的用户提供了登录页面,并且用户进行了身份validation。 但是,当尝试访问url /个人时,我得到403错误,这在日志中

 2015-11-12 13:59:46.373 DEBUG 34468 --- [nio-8090-exec-6] osswumatcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/' 2015-11-12 13:59:46.373 DEBUG 34468 --- [nio-8090-exec-6] osswumatcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/login' 2015-11-12 13:59:46.373 DEBUG 34468 --- [nio-8090-exec-6] osswumatcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/css/**' 2015-11-12 13:59:46.373 DEBUG 34468 --- [nio-8090-exec-6] osswumatcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/js/**' 2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] osswumatcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/img/**' 2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] osswumatcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/bootstrap/**' 2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] osswumatcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/individual' 2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] osswaiFilterSecurityInterceptor : Secure object: FilterInvocation: URL: /individual; Attributes: [hasRole('ROLE_USER')] 2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] osswaiFilterSecurityInterceptor : Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@4c2eda81: Principal: org.springframework.security.core.userdetails.User@4c0ab982: Username: foo@bar.com; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffc7f0c: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 6E0BF830C070912EAC0050D1285D5CF9; Granted Authorities: USER 2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] ossaccess.vote.AffirmativeBased : Voter: org.springframework.security.web.access.expression.WebExpressionVoter@6f192fd7, returned: -1 2015-11-12 13:59:46.386 DEBUG 34468 --- [nio-8090-exec-6] osswaExceptionTranslationFilter : Access is denied (user is not anonymous); delegating to AccessDeniedHandler 

如果我用.hasRole("USER")替换.hasRole("USER")它可以工作,并且用户获得所需的页面,但没有检查任何角色。

我无法弄清楚如何指定用户必须经过身份validation并具有USER角色。 我已经看了很多例子并尝试了很多组合,但我无法让它按照需要运行。 大多数示例也引用了较旧的基于XML的配置,我想避免这些配置。

如何指定可以使用具有特定角色的经过身份validation的用户访问某些URL模式,我需要做什么?

根据要求,成功处理程序方法如下所示

 @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth) throws IOException, ServletException { // Here we want to check whether the user has already started the process and redirect to the appropriate stage log.info("User {} has been authenticated", auth.getName()); if(auth.getName().equals("foo@bar.com")) { redirectStrategy.sendRedirect(request, response, "/individual"); return; } redirectStrategy.sendRedirect(request, response, "/application"); } 

请注意,根据代码中的注释,这是早期的WIP。 我知道这有效,因为日志消息出现,并且在WebSecurity类中没有角色检查所提供的页面。

编辑:在注意到roleService.getDefaultRole()的问题中注意到一个拼写错误。 这是一个错误,并已得到纠正。

我认为您登录的用户foo@bar.com设置了错误的权限。 在日志中,您可以看到权限是[‘USER’],但由于您正在使用角色,因此它应该是[‘ROLE_USER’]。 你在哪里定义foo@bar.com的权限?

否则尝试切换

 .antMatchers("/individual", "/application", "/upload").hasRole("USER") 

 .antMatchers("/individual", "/application", "/upload").hasAuthority("USER") 

修改你的配置是这样的。

 http.authorizeRequests().antMatchers("/individual","/application", "/upload") .hasRole("USER").and().authorizeRequests().anyRequest() .authenticated().and() .formLogin() .loginPage("/login") .successHandler(successHandler); 

如果您不希望对所有请求进行身份validation,则可以使用anyRequest()替换anyRequest()方法antMatchers("/urls-goes-here")

我已经解决了我自己的问题,部分归功于这里的各种建议让我对它进行了不同的研究。 还注意到FrontierPsychiatrist也提供了答案,但在我开展工作时我没有发现响应。

问题是spring可以互换地提到了角色和权威。

实现UserDetailsService接口以允许Spring从应用程序数据库加载用户记录后,我创建了org.springframework.security.core.userdetails.User的实例并设置了授予的权限 。 在日志语句中可以看到User对象具有 – Granted Authorities: USER ,但是我的安全配置正在检查角色,并且失败。 简单来说,解决方案是检查权限。 我还清理了配置,将要忽略的URL模式与身份validation和授权分开。 完整的代码片段如下

 @Override public void configure(WebSecurity web) throws Exception { web .ignoring() .antMatchers( "/css/**", "/js/**", "/img/**", "/bootstrap/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/individual","/application", "/upload").hasAuthority("USER") .and() .formLogin() .loginPage("/login") .successHandler(successHandler) .permitAll() .and() .logout() .deleteCookies("remove") .logoutSuccessUrl("/login?logout") .permitAll(); } 

我真的希望在Spring 4 MVC中使用基于Java的配置时,这有助于其他人。

这是一个教程,可以帮助您实现以下目标:

http://www.jcombat.com/spring/custom-authentication-success-handler-with-spring-security

我猜你忘了在你的处理程序中设置会话属性:

  HttpSession session = request.getSession(); session.setAttribute("uname", authUser.getUsername()); session.setAttribute("authorities", authentication.getAuthorities());