201411167 / springboot-security

스프링 시큐리티 구현
0 stars 0 forks source link

WebSecurityConfig.java 에러 이슈 #2

Closed 201411167 closed 4 years ago

201411167 commented 4 years ago

[에러] Error creating bean with name 'springSecurityFilterChain' nested exception is java.lang.IllegalArgumentException: role should not start with 'ROLE_' since it is automatically inserted.

[원인] http.authorizeRequests().antMatchers("/admin/**").hasRole("ROLEADMIN") 부분에서 role의 값은 'role'로 시작하는 값을 받을 수 없다. 원한다면 "ADMIN" 등의 값을 넣어주면 된다.

[코드]

// ExceptionUrlAuthorizationConfigurer.class 
private static String hasRole(String role) {
        Assert.notNull(role, "role cannot be null");
        if (role.startsWith("ROLE_")) {
            throw new IllegalArgumentException("role should not start with 'ROLE_' since it is automatically inserted. Got '" + role + "'");
        } else {
            return "hasRole('ROLE_" + role + "')";
        }
    }