Spring Boot Role Based Authentication

守給你的承諾、 提交于 2019-12-06 05:31:38

Can you please check this.

withUser("user1").password("password1").roles("USER", "ADMIN")

write "USER" and "ADMIN" in separate qoutes.

I changed it in the following way, now it seems to be working:

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

     @Autowired
     protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
         auth.inMemoryAuthentication()
            .withUser("user1").password("password1").roles("USER", "ADMIN")
            .and()
            .withUser("user2").password("password2").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().permitAll()
            .and()
            .authorizeRequests()
            .antMatchers("/service/test").hasAnyRole("USER", "ADMIN")
            .antMatchers("/service/admin").hasRole("ADMIN")
            .anyRequest().authenticated();
    }
}

Thank you very much for your answers!

following setup works fine for my spring boot app:

http.authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()//allow CORS option calls
            .antMatchers("/home", "/").hasAnyAuthority(Role.ROLE_ADMIN, Role.ROLE_USER)
            .antMatchers("/admin").hasAuthority(Role.ROLE_ADMIN)enter code here
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!