SpringBoot

spring security 访问403 Forbidden

2018-10-12  本文已影响0人  Epiphanic

spring security 配置了访问权限管控

protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http);
        // 定制请求的授权规则
        http
            .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/user/**").hasRole("USER")
                .antMatchers("/admin/**").hasRole("ADMIN")

然后前端登陆,访问需要权限的地址时出现

403 Forbidden

后台代码调试发现,报错信息如下:(拒绝访问(用户不是匿名的);委托给Access Dead处理程序)

2018-10-12 16:22:30.101 DEBUG 2200 --- [http-nio-8080-exec-7] o.s.s.w.a.ExceptionTranslationFilter     :
    Access is denied (user is not anonymous); delegating to AccessDeniedHandler
    
org.springframework.security.access.AccessDeniedException: Access is denied
    at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:84) ~[spring-security-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]

即用户没有访问的权限。

然后将调试信息往上翻了一下发现了如下内容:

2018-10-12 16:31:56.335 DEBUG 2200 --- [http-nio-8080-exec-7] o.s.s.w.a.i.FilterSecurityInterceptor    : Secure object: FilterInvocation: URL: /user; Attributes: [hasRole('ROLE_USER')]

2018-10-12 16:22:30.099 DEBUG 2200 --- [http-nio-8080-exec-7] o.s.s.w.a.i.FilterSecurityInterceptor    : Secure object: FilterInvocation: URL: /admin/; Attributes: [hasRole('ROLE_ADMIN')]

原来他会把设置的角色名自动加上 ‘ ROLE_ ’ 前缀。

所以在数据库中将角色名设置为带 ‘ ROLE_ ’ 前缀的值便能成功访问限制的地址

id role_name
1 ROLE_ADMIN
2 ROLE_USER

也可以在取值后添加 ‘ ROLE_ ’ 通过验证

上一篇 下一篇

猜你喜欢

热点阅读