역할 계층 구조에 관한 많은 스레드가 있지만 OAuth2와 결합 된 예제를 찾을 수 없습니다.스프링 부트를 사용한 역할 계층 구조 및 OAuth2 보안
그래서, 스레드의 대부분은 내가 RoleHierarchy
콩 구현하는 데 필요한 포인트 :
Beans.java
@EnableJpaRepositories(basePackages = "com.template.service.repository")
@EnableAspectJAutoProxy
@ComponentScan
@Configuration
public class Beans {
@Bean
public ItemService itemsService(ItemsRepository itemsRepository) {
return new ItemService(itemsRepository);
}
@Bean
public RoleHierarchy roleHierarchy(){
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
roleHierarchy.setHierarchy("ROLE_SUPREME > ROLE_DEVELOPER ROLE_DEVELOPER > ROLE_ADMIN ROLE_ADMIN > ROLE_USER");
return roleHierarchy;
}
@Bean
public DtoMapper dtoMapper() {
return new DtoMapper();
}
}
다음, 나는 @Autowire
내 WebSecurityConfigurerAdapter
이 빈을해야합니다. 그러나 OAuth2 보안을 사용하고 있으므로 HttpSecurity
이 ResourceServerConfigurerAdapter
내부에 구성되어 있습니다.
OAuth2.java
public class OAuth2 {
@EnableAuthorizationServer
@Configuration
@ComponentScan
public static class AuthorizationServer extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManagerBean;
@Autowired
private UserDetailsService userDetailsService;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("trusted_client")
.authorizedGrantTypes("password", "refresh_token")
.scopes("read", "write");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManagerBean).userDetailsService(userDetailsService);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.allowFormAuthenticationForClients();
}
}
@EnableResourceServer
@Configuration
@ComponentScan
public static class ResourceServer extends ResourceServerConfigurerAdapter {
@Autowired
private RoleHierarchy roleHierarchy;
private SecurityExpressionHandler<FilterInvocation> webExpressionHandler() {
OAuth2WebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new OAuth2WebSecurityExpressionHandler();
defaultWebSecurityExpressionHandler.setRoleHierarchy(roleHierarchy);
return defaultWebSecurityExpressionHandler;
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().expressionHandler(webExpressionHandler())
.antMatchers("/api/**").hasRole("DEVELOPER");
}
}
}
@EnableWebSecurity @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) @Configuration @ComponentScan public class Security extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Bean public JpaAccountDetailsService userDetailsService(AccountsRepository accountsRepository) { return new JpaAccountDetailsService(accountsRepository); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Bean public PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } }
그러나 계층 구조가 작동하지 Security.java
. SUPREME 사용자의 자격 증명 요청으로 끝 : 나는hasRole("DEVELOPER")
hasRole("SUPREME")
로 전환
{
"error": "access_denied",
"error_description": "Access is denied"
}
- 모든 것이 잘 작동합니다. 역할 계층 구조 -
나는 봄 부팅 1.5.2 그리고 봄 보안 OAuth를 2.1.0.RELEASE
UPDATE 내가 모든 OAuth2.java
클래스를 언급하고 Security.java
클래스에 webExpressionHandler()
방법 서명을 이동
을 사용하고 있습니다 잘 작동합니다. 그렇다면 OAuth2 Resource Server는 어떻게 될 것입니까?