每当宣布停止请求时,HasAnyAuthority 总是让我进入 api

bie*_*las 2 authentication authorization spring-security

通过 Spring Security 我创建了一个方法:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
            .csrf().disable()
            .authorizeRequests()
                .antMatchers("/static/build/app.js", "/static/app/styles/*/**", "/static/app/js/*/**",
                        "/static/build/libs.js", "/index.html", "/static/build/*/**", "/", "/static/**").permitAll()
                .antMatchers("/auth/**").permitAll()
                .antMatchers("/api/user/registerClient").permitAll()
                .antMatchers("/api/user/checklogin/**").permitAll()
                .antMatchers("/api/user/getAllAdmins").permitAll()
                .antMatchers("/api/**").hasAnyAuthority(AuthoritiesConstants.CLIENT, AuthoritiesConstants.ADMIN, AuthoritiesConstants.WORKER)
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/")
                .loginProcessingUrl("/")
                .permitAll();
Run Code Online (Sandbox Code Playgroud)

和控制器方法的例子:

@RequestMapping(value = "/api/vehicle")
@PreAuthorize("hasAnyAuthority('ADMIN', 'CLIENT')")
@RequestMapping(value = "", method = RequestMethod.GET)
public List<VehicleReservationModel> getVehiclesForClientByLogin(HttpServletRequest request) {
    Principal name = request.getUserPrincipal();
    if (name.getName() == null) {
        throw new RuntimeException("Brak sesji");
    }
    if (roleService.getRoleForUserByLogin(name.getName()).toLowerCase().equals("admin")) {
        return vehicleService.getAllVehicles();
    } else {
        List<VehicleReservationModel> vehicleList = vehicleService.getVehiclesForClientByLogin(name.getName());
        if (vehicleList == null) {
            throw new RuntimeException("Brak pojazdów dla klienta " + name.getName() + " - lista jest pusta");
        }
        return vehicleList;
    }
}
Run Code Online (Sandbox Code Playgroud)

这种情况是每当我ADMIN

@PreAuthorize("hasAnyAuthority('ADMIN', 'CLIENT')")
Run Code Online (Sandbox Code Playgroud)

并评论:

.antMatchers("/api/**").hasAnyAuthority(AuthoritiesConstants.CLIENT, AuthoritiesConstants.ADMIN, AuthoritiesConstants.WORKER)
Run Code Online (Sandbox Code Playgroud)

总是让我进入 API。我想每当我创建一些特权时,它总是有效的。为什么在上面的例子中我的 Spring Security 不起作用?

更新: answear 是启用使用PreAuthorize您需要添加的注释: @EnableGlobalMethodSecurity(prePostEnabled = true)

dur*_*dur 5

您启用@SecuredEnableGlobalMethodSecurity#securedEnabled

确定是否应启用 Spring Security 的 Secured 注释。

但你必须启用@PreAuthorizeEnableGlobalMethodSecurity#prePostEnabled

确定是否应启用 Spring Security 的 pre post annotations。默认为假。

您修改后的 Spring Security 配置:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
            .csrf().disable()
            .authorizeRequests()
                .antMatchers("/static/build/app.js", "/static/app/styles/*/**", "/static/app/js/*/**",
                    "/static/build/libs.js", "/index.html", "/static/build/*/**", "/", "/static/**").permitAll()
                .antMatchers("/auth/**").permitAll()
                .antMatchers("/api/user/registerClient").permitAll()
                .antMatchers("/api/user/checklogin/**").permitAll()
                .antMatchers("/api/user/getAllAdmins").permitAll()
                // .antMatchers("/api/**").hasAnyAuthority(AuthoritiesConstants.CLIENT, AuthoritiesConstants.ADMIN, AuthoritiesConstants.WORKER)
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/")
                .loginProcessingUrl("/")
                .permitAll(); 
Run Code Online (Sandbox Code Playgroud)