SpringBoot报错-role should not start with ‘ROLE_’ since it is automatically inserted. Got ‘ROLE_USER’

本文引用-role should not start with ‘ROLE_’ since it is automatically inserted. Got ‘ROLE_ADMIN’

百度了一番发现并没人遇到此问题,但我可是照着《Spring实战》一笔一划敲上去的,于是换了必应搜索,才看到国外某论坛有人提出了解决方案,其实报错信息已经描述地明明白白了 T_T
role不应该以’ ROLE_ '开头,因为它会自动插入。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin", "/user")
                	.hasRole("ROLE_USER")	//将此处ROLE_USER改成USER即可
                .antMatchers("/", "/**")
                	.permitAll();
    }
}

改后如下列代码所示:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin", "/user")
                	.hasRole("USER")
                .antMatchers("/", "/**")
                	.permitAll();
    }
}
posted @ 2021-01-11 02:29  Coder-Jiang  阅读(27)  评论(0)    收藏  举报  来源