Tôi đã cố gắng triển khai máy chủ xác thực OAuth2 bằng cách sử dụng hướng dẫn của Dave Syer với một số nguồn cảm hứng từ JHipster. Nhưng tôi không thể tìm ra cách nó hoạt động cùng nhau.Bảo mật mùa xuân OAuth2, quyết định bảo mật?
Có vẻ như thiết lập bảo mật bằng cách sử dụng WebSecurityConfigurerAdapter được ghi đè khi tôi sử dụng ResourceServerConfigurerAdapter.
@Configuration
@EnableResourceServer
public class OAuth2ResourceConfig extends ResourceServerConfigurerAdapter {
private TokenExtractor tokenExtractor = new BearerTokenExtractor();
@Override
public void configure(HttpSecurity http) throws Exception {
http
.addFilterAfter(contextClearer(), AbstractPreAuthenticatedProcessingFilter.class)
.authorizeRequests()
.anyRequest().authenticated().and().httpBasic();
}
private OncePerRequestFilter contextClearer() {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (tokenExtractor.extract(request) == null) {
SecurityContextHolder.clearContext();
}
filterChain.doFilter(request, response);
}
};
}
@Component
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
private final AuthenticationManager authenticationManager;
@Autowired
public CustomWebSecurityConfigurerAdapter(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.parentAuthenticationManager(authenticationManager);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage("/login").permitAll()
.and()
.authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.and()
.requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests().anyRequest().authenticated();
}
}
Đây là mã được lấy từ một vài ví dụ khác nhau, để chúng có thể không được trộn lẫn. Nhưng tôi không thể tìm thấy một danh sách tài liệu/ví dụ tốt cho OAuth2 (không giống như Spring Boot có tài liệu tuyệt vời), vì vậy tôi gặp sự cố khi hiểu được cách thức tất cả chúng phù hợp với nhau. Nếu tôi không thêm loginForm vào ResourceServerConfigurerAdapter, nó sẽ cho tôi trái phép. Nhưng tôi đã định nghĩa nó trong WebSecurityConfigurererAdapter dưới dạng allowAll().
Đây là AuthorizationServerConfigurerAdapter:
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private JwtAccessTokenConverter jwtAccessTokenConverter;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("acme")
.secret("acmesecret")
.authorizedGrantTypes("authorization_code", "refresh_token",
"password").scopes("openid");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager).accessTokenConverter(jwtAccessTokenConverter);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
}
}
Bất cứ điều gì tôi đang làm sai? Tôi có phải thiết lập tất cả bảo mật trong ResourceServerConfigurerAdapter không? Thậm chí tôi có cần đến WebSecurityConfigurerAdapter nữa không?
Nếu có ai biết bất kỳ hướng dẫn, hướng dẫn, blog hoặc bất cứ thứ gì giống nhau có thể giúp tôi cuốn đầu về cách thức hoạt động của nó, điều đó sẽ được đánh giá cao.
Trân trọng, Kenneth.
'OAuth2ResourceConfig' của bạn là dư thừa theo như tôi có thể thấy. Chỉ cần nói. –
Các triệu chứng (bạn đang đánh đường nào và bạn thấy gì)? Sử dụng curl (-v để xem tiêu đề) và đăng nhập DEBUG cho Spring Security sẽ cho bạn biết mọi thứ bạn cần biết. –
Các triệu chứng là về cơ bản nó bỏ qua WebSecurityConfigurerAdapter. Nhưng sau khi đọc cho bạn lời giải thích dưới đây, tôi sẽ hiểu thêm một chút về cách thức hoạt động của nó. – LG87