小编sil*_*awk的帖子

Spring Security,Method Security annotation(@Secured)无效(java config)

我正在尝试使用@Secured("ADMIN")设置方法安全注释(没有任何XML,只有java配置,Spring Boot).但是通过角色访问不起作用.

安全配置:

@Configuration
@EnableWebSecurity
public class AppSecurityConfiguration extends WebSecurityConfigurerAdapter{

.....

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/api/**").fullyAuthenticated().and()
                .addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    }

.....

}
Run Code Online (Sandbox Code Playgroud)

我想限制访问控制器的方法:

@RestController
@RequestMapping("/api/groups")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @Secured("ADMIN")
    @RequestMapping
    public List<Group> list() {
        return groupService.findAll();
    }

}
Run Code Online (Sandbox Code Playgroud)

通过网址限制访问是有效的,具有:

.antMatchers("/api/**").hasAuthority("ADMIN")
Run Code Online (Sandbox Code Playgroud)

也许我忘了指定我想要按角色限制?

UPD: 根据规则,在哪一层必须@PreAuthorize("hasRole('ADMIN')")在Controller层或Service层中?

java security spring spring-security spring-boot

17
推荐指数
4
解决办法
3万
查看次数

标签 统计

java ×1

security ×1

spring ×1

spring-boot ×1

spring-security ×1