2017-03-13 1 views
1

스프링 부트 역할 기반 인증과 관련된 문제가 있습니다. 기본적으로 사용자와 관리자가 있어야하며 사용자가 관리자 리소스에 액세스하지 못하도록하고 싶습니다.스프링 부트 역할 기반 인증

package test; 

import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; 

@Configuration 
@EnableWebMvcSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(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.authorizeRequests() 
      .antMatchers("/service/test").access("hasRole('USER') or hasRole('ADMIN')") 
      .antMatchers("/service/admin").access("hasRole('ADMIN')"); 
    } 
} 

이 내 작은 REST 서비스입니다 :

package test; 

import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
@RequestMapping("/service") 
public class RestService { 

    @RequestMapping(value = "/test", method = RequestMethod.GET) 
    public String echo() { 
     return "This is a test"; 
    } 

    @RequestMapping(value = "/admin", method = RequestMethod.GET) 
    public String admin() { 
     return "admin page"; 
    } 
} 

그리고 내 응용 프로그램 클래스 : 그래서 나는의 SecurityConfig 클래스 생성

package test; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
@EnableAutoConfiguration 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

불행하게도, 나는 항상 403 "금지를 얻을 수를/curl user1 : password1 @ localhost : 8080/service/admin "을 실행할 때"액세스가 거부되었습니다. "라는 오류 메시지가 나타납니다.

미리 감사드립니다.

+0

당신은 할 수 있습니까 "가 password1의 @ localhost를 : USER1를 8080/서비스/테스트"? –

+0

아니요, 모든 엔드 포인트에 대한 오류 메시지가 나타나는 것 같습니다. –

답변

2

확인할 수 있습니까?

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

쓰기 "USER"별도의 qoutes에서 "ADMIN".

+0

불행히도, 아직 작동하지 않습니다 ... :( –

1

나는 다음과 같은 방법으로 변경, 지금은 작동하는 것 같군 :

@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(); 
    } 
} 

가 귀하의 답변 주셔서 감사합니다!

0

다음 설치 나의 봄 부트 앱 잘 작동 :

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