java - 在 SpringBoot 应用程序中使用 @RolesAllowed 的异常

标签 java spring spring-boot spring-security thymeleaf

我有一个基本的 SpringBoot 应用程序。使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎,并将其打包为可执行的 JAR 文件。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
}

companyService 被注入(inject)并且不为空。删除 @RolesAllowed工作正常
@Autowired
CompanyService companyService;

在我的应用程序配置中:
@Configuration
@EnableGlobalMethodSecurity(jsr250Enabled=true, securedEnabled=true, prePostEnabled=true)

我有一个这样注释的 Controller 方法
@ModelAttribute("companies")
    @RolesAllowed({"ROLE_ADMIN"})
    public Iterable<Company> companies(){
        return companyService.findAll();
    }

当我尝试访问 Controller 时,我遇到了一个没有任何信息的应用程序异常:
<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

    -->

在到达 Controller 之前,我检查了用户的角​​色
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
  • 删除@Secured 和调试AffirmativeBased 时调用company()我有 :

    开关(结果){
    案例 AccessDecisionVoter.ACCESS_GRANTED:
    返回;
    logger.debug("授权成功");
  • 最佳答案

    不要使用@Secured@RolesAllowed不再推荐使用此注释。而是使用 @PreAuthorize("hasAuthority('ROLE_ADMIN')")

    关于java - 在 SpringBoot 应用程序中使用 @RolesAllowed 的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44085969/

    相关文章:

    java - 使用NamedParameterJdbcTemplate批量插入

    javascript - 在 onclick 属性中使用 thymeleaf 变量

    java - 访问 hashmap 的值

    java - TextView 未显示/相对布局

    java - 通过 spring websocket、sockJs 和 STOMP 向经过身份验证的用户发送通知

    java - 创建新的自定义对象与在 Java 中将同一对象的多个值设置为 Null

    java - Spring JPA native 查询调用存储过程给出 “No converter found capable of converting from type”

    java - JpaRepository 不更新实体

    java - 使用 Libgdx 进行高效 3D block 渲染

    java - 通过解码 Base64 播放音频