Tôi đã dành một chút thời gian gói đầu xung quanh bảo mật mùa xuân khi sử dụng cấu hình java thuần túy. Có một vài bước liên quan đến việc làm việc này. Nó phải là một cái gì đó dọc theo những dòng này. Quá trình cơ bản như sau:
Tạo bộ lọc tùy chỉnh để kiểm tra yêu cầu cung cấp thông tin ủy quyền cụ thể
Mỗi bộ lọc trả về null (nếu không có uỷ quyền của loại được tìm thấy), hoặc một tùy chỉnh AbstractAuthenticationToken
Nếu bộ lọc trả về mã thông báo, mỗi phương thức hỗ trợ (lớp) của AuthenticationProvider sẽ được gọi với mã thông báo đó trả về true | false nếu nó nên thử xác thực
tryAuthentication sau đó sẽ được gọi trên AuthenticationProvider hỗ trợ mã thông báo. Ở đây bạn thực hiện bất kỳ cuộc gọi dịch vụ nào để xác thực người dùng. Sau đó bạn có thể ném LoginException hoặc gọi authentication.setAuthenticated (true) và trả lại mã thông báo để xác thực thành công.
Tôi đã sử dụng thiết lập này một lúc để hỗ trợ nhiều phương thức xác thực khác nhau (yêu cầu đã ký tên, tên người dùng/mật khẩu, oauth v.v.) và hoạt động khá tốt.
Bạn cũng có thể chuyển AuthenticationSuccessHandler và AuthenticationFailuersHandler cho các bộ lọc bảo mật tùy chỉnh để cung cấp các chiến lược chuyển hướng tùy chỉnh và xử lý lỗi.
Ngoài ra, hãy đảm bảo thiết lập trình so khớp kiến trong các hàm tạo của bộ lọc để kiểm soát các mẫu url mà bộ lọc cũng áp dụng. Ví dụ, một bộ lọc yêu cầu ldap có lẽ sẽ cần phải được kiểm tra với bất kỳ yêu cầu "/ *" trong khi một bộ lọc tên người dùng/mật khẩu chỉ có thể được kiểm tra trên POST để/đăng nhập hoặc một cái gì đó tương tự.
Ví dụ Code:
1) Tạo tùy chỉnh AuthenticationToken cho từng loại chứng thực bạn muốn hỗ trợ
public class LDAPAuthorizationToken extends AbstractAuthenticationToken {
private String token;
public LDAPAuthorizationToken(String token) {
super(null);
this.token = token;
}
public Object getCredentials() {
return token;
}
public Object getPrincipal() {
return null;
}
}
public class OTPAuthorizationToken extends UsernamePasswordAuthenticationToken {
private String otp;
public OTPAuthorizationToken(String username, String password, String otp) {
super(username, password);
this.otp = otp;
}
public String getOTP() {
return otp;
}
}
2) Tạo bộ lọc an ninh tùy chỉnh cho từng loại
public class LDAPAuthorizationFilter extends AbstractAuthenticationProcessingFilter {
@Autowired
private UserDetailsService userDetailsService;
public LDAPAuthorizationFilter() {
super("/*"); // allow any request to contain an authorization header
}
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException
{
if (request.getHeader("Authorization") == null) {
return null; // no header found, continue on to other security filters
}
// return a new authentication token to be processed by the authentication provider
return new LDAPAuthorizationToken(request.getHeader("Authorization"));
}
}
public class OTPAuthorizationFilter extends AbstractAuthenticationProcessingFilter {
@Autowired
private UserDetailsService userDetailsService;
public OTPAuthorizationFilter() {
super("/otp_login");
}
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException
{
if (request.getParameter("username") == null || request.getParameter("password") == null || request.getParameter("otp") == null) {
return null;
}
// return a new authentication token to be processed by the authentication provider
return new OTPAuthorizationToken(request.getParameter("username"), request.getParameter("password"), request.getParameter("otp"));
}
}
3) Tạo Trình xác thực tùy chỉnh
public class LDAPAuthenticationProvider implements AuthenticationProvider {
@Autowired
private MyAuthenticationService sampleService;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
LDAPAuthorizationToken auth = (LDAPAuthorizationToken)authentication;
String username = sampleService.verifyToken(auth.getCredentials());
if (username == null) {
throw new LoginException("Invalid Token");
}
auth.setAuthenticated(true);
return auth;
}
@Override
public boolean supports(Class<?> authentication) {
if (authentication.isAssignableFrom(LDAPAuthorizationToken.class)) {
return true;
}
return false;
}
}
public class OTPAuthenticationProvider implements AuthenticationProvider {
@Autowired
private MyAuthenticationService sampleService;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
OTPAuthorizationToken auth = (OTPAuthorizationToken)authentication;
String error = sampleService.loginWithOTP(auth.getPrincipal(), auth.getCredentials(), auth.getOTP());
if (error != null) {
throw new LoginException(error);
}
auth.setAuthenticated(true);
return auth;
}
@Override
public boolean supports(Class<?> authentication) {
if (authentication.isAssignableFrom(OTPAuthorizationToken.class)) {
return true;
}
return false;
}
}
4) Cấu hình bảo mật mùa xuân
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// configure filters
http.addFilterBefore(new LDAPAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
http.addFilterBefore(new OTPAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
// configure authentication providers
http.authenticationProvider(new LDAPAuthenticationProvider());
http.authenticationProvider(new OTPAuthenticationProvider());
// disable csrf
http.csrf().disable();
// setup security
http.authorizeRequests()
.anyRequest()
.fullyAuthenticated()
.and().httpBasic();
}
}
Hy vọng rằng sẽ giúp!
nó hoạt động? Khóa tiêu đề "Ủy quyền" là gì? URL auth là gì? Cảm ơn – tayfun