未知的名称值ROLE_USER,ADMIN,REVIEWER

时间:2017-11-26 14:58:00

标签: java mysql spring spring-security enums

  

org.springframework.security.authentication.InternalAuthenticationServiceException:枚举类的未知名称值[reviewer] [com.musicreview.model.UserRole];嵌套异常是java.lang.IllegalArgumentException:枚举类的未知名称值[reviewer] [com.musicreview.model.UserRole]

public enum UserRole {
    ADMIN, USER, REVIEWER;

    @Override
    public String toString() {
        return "ROLE_" + name();
    }
}

我添加了userrole模型类

   @Column(name = "user_role")
    @Enumerated(EnumType.STRING)
    private UserRole role;

和安全配置

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

    @Autowired
    public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(userDetailsService)
                .passwordEncoder(getShaPasswordEncoder());
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/").hasAnyRole("USER", "ADMIN","REVIEWER")
                .antMatchers("/admin").hasRole("ADMIN")
                .antMatchers("/register").permitAll()
                .and()
        .exceptionHandling().accessDeniedPage("/unauthorized")
                .and()
        .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/j_spring_security_check")
                .failureUrl("/login?error")
                .usernameParameter("j_login")
                .passwordParameter("j_password")
                .permitAll() //пускать всех
                .and()
        .logout()
                .permitAll()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login?logout")
                .invalidateHttpSession(true);
    }

    private ShaPasswordEncoder getShaPasswordEncoder(){
        return new ShaPasswordEncoder();
    }
}

有什么问题?

0 个答案:

没有答案