【发布时间】:2017-10-20 12:34:10
【问题描述】:
我有一个基本的 SpringBoot 应用程序。使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为可执行 JAR 文件。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
}
companyService 已注入且不为空。删除 @RolesAllowed 工作正常
@Autowired
CompanyService companyService;
在我的应用配置中:
@Configuration
@EnableGlobalMethodSecurity(jsr250Enabled=true, securedEnabled=true, prePostEnabled=true)
我有一个这样注释的控制器方法
@ModelAttribute("companies")
@RolesAllowed({"ROLE_ADMIN"})
public Iterable<Company> companies(){
return companyService.findAll();
}
当我尝试访问控制器时,我遇到了一个没有任何信息的应用程序异常:
<div th:utext="'Failed URL: ' + ${url}" th:remove="tag">${url}</div>
<div th:utext="'Exception: ' + ${message}" th:remove="tag">${message}</div>
<div th:utext="'Exception: ' + ${trace}" th:remove="tag">${trace}</div>
<!--
Failed URL: null
Exception: No message available
Exception: null
-->
在到达控制器之前,我会检查用户的角色
System.out.println("Authorities -> " +
SecurityContextHolder.getContext().getAuthentication().getAuthorities())
结果如下:
Authorities -> [Authority [authority=ROLE_BASIC], Authority [authority=ROLE_ADMIN]]
使用相同的结果:
@ModelAttribute("companies")
@Secured("ADMIN")
public Iterable<Company> companies(){
return companyService.findAll();
}
或@Secured("ROLE_ADMIN")
在调试中:
42410 [http-nio-8080-exec-7] DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@65eab2b2, returned: 1
42410 [http-nio-8080-exec-7] DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Authorization successful
42410 [http-nio-8080-exec-7] DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - RunAsManager did not change Authentication object
42410 [http-nio-8080-exec-7] DEBUG o.s.security.web.FilterChainProxy - /company/list reached end of additional filter chain; proceeding with original chain
42411 [http-nio-8080-exec-7] DEBUG o.s.s.w.a.ExceptionTranslationFilter - Chain processed normally
42411 [http-nio-8080-exec-7] DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
42411 [http-nio-8080-exec-7] DEBUG o.a.c.c.C.[Tomcat].[localhost] - Processing ErrorPage[errorCode=0, location=/error
-
companies() 会在您删除 @Secured 时调用,并调试
AffirmativeBased我得到了:切换(结果){ 案例 AccessDecisionVoter.ACCESS_GRANTED: 返回; logger.debug("授权成功");
【问题讨论】:
-
日志中没有堆栈跟踪吗?
-
不,日志中没有堆栈跟踪
-
您的应用是否要求用户身份验证? (即浏览器要求用户/密码)当您访问您的安全网址时?
-
是的,我的应用要求用户认证
-
您是否也有
@EnableWebSecurity,并正确配置了AuthenticationManagerBuilder?
标签: java spring spring-boot spring-security thymeleaf