【问题标题】:Authorization roles Spring-boot Oauth2 ~ Restful API授权角色 Spring-boot Oauth2 ~ Restful API
【发布时间】:2016-05-13 13:31:36
【问题描述】:

我需要帮助解决这个问题... 我无法在我的安全配置文件中保护我的控制器。但我可以在我的控制器中使用

@PreAuthorize("hasAuthority('ROLE_ADMIN')")

但这真的很烦人,我想从我的安全配置中做到这一点。文件

这是我的 WebSecurityconfigurerAdapter:

@Configuration
//@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = false)
//@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
//@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    CustomAuthenticationProvider customAuthenticationProvider;

    @Autowired
    CustomUserDetailsService cuds;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(cuds)
                .passwordEncoder(passwordEncoder())
                .and()
                .authenticationProvider(customAuthenticationProvider);
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/**").authenticated()
                .antMatchers("/test").authenticated()
                .antMatchers("/usuarios/**").hasRole("ADMIN");
    }
}

这是我的 Oauth2Configuration:

@Configuration
public class Oauth2Configuration {

    private static final String RESOURCE_ID = "restservice";

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

        @Autowired
        private CustomLogoutSuccessHandler customLogoutSuccessHandler;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources) {
            resources
                    .resourceId(RESOURCE_ID);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                    // Logout
                    .logout()
                    .logoutUrl("/oauth/logout")
                    .logoutSuccessHandler(customLogoutSuccessHandler)
                    .and()
                    //Session management
                    .sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    //URI's to verify
                    .authorizeRequests()
                    .antMatchers("/oauth/logout").permitAll()
                    .antMatchers("/**").authenticated()
                    .antMatchers("/usuarios/**").hasRole("ADMIN");
        }
    }

我尝试使用权限和角色,但没有任何效果。知道我做错了什么吗?

【问题讨论】:

  • 可能是订购问题?如果你把.antMatchers("/usuarios/**").hasRole("ADMIN") 放在.antMatchers("/**").authenticated() 之前会怎样
  • 男人,我爱你!!哈哈,是订单有问题

标签: spring spring-security oauth-2.0 spring-boot


【解决方案1】:

感谢 Yannic Klem,我得到了答案,是订单有问题

首先在我的WebSecurityConfigurerAdapter 上,我将身份验证设置为“usuarios”

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/usuarios").authenticated();
    }

之后在我的Oauth2Configuration 中设置我的权限和我的角色。

@Override
    public void configure(HttpSecurity http) throws Exception {
        http
                // Logout
                .logout()
                .logoutUrl("/oauth/logout")
                .logoutSuccessHandler(customLogoutSuccessHandler)
                .and()
                //Session management
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                //URI's to verify
                .authorizeRequests()
                .antMatchers("/oauth/logout").permitAll()                    
                .antMatchers("/usuarios/**").hasRole("ADMIN");
    }

现在一切正常。谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-04
    • 2021-08-02
    • 1970-01-01
    • 2016-04-10
    • 2013-12-18
    • 1970-01-01
    • 2020-09-13
    • 2015-04-21
    相关资源
    最近更新 更多