Дешифруя имя пользователя и пароль перед аутентификацией Spring Boot Start AuthenticationJAVA

Программисты JAVA общаются здесь
Anonymous
 Дешифруя имя пользователя и пароль перед аутентификацией Spring Boot Start Authentication

Сообщение Anonymous »

Я создал этот фильтр для расшифровки имени пользователя и пароля, а затем Spring Boot может продолжить аутентификацию пользователя < /p>
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import jakarta.servlet.http.HttpServletRequest;
import com.example.demo.controller.HybridController;

public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

public CustomAuthenticationFilter(AuthenticationManager authenticationManager) {
super.setAuthenticationManager(authenticationManager);

}

HybridController hybridController = new HybridController();

private String decrypt(String data) {
try {
return hybridController.Hybrid_Data_Decryption(data);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

@Override
protected String obtainPassword(HttpServletRequest request) {
String decPassword = decrypt(super.obtainPassword(request));
return decPassword;
}

@Override
protected String obtainUsername(HttpServletRequest request) {
String decUsername = decrypt(super.obtainUsername(request));
return decUsername;
}

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {

String username = obtainUsername(request);
username = (username != null) ? username.trim() : "";
String password = obtainPassword(request);
password = (password != null) ? password : "";

Admin admin = adminRepository.findByUsername(username.toLowerCase()).orElse(null);
Authentication authentication = null;
if (admin != null) {
authentication = this.getAuthenticationManager()
.authenticate(new UsernamePasswordAuthenticationToken(username,
password, mapRolesToAuthorities(admin.getRoles())));
}

SecurityContextHolder.getContext().setAuthentication(authentication);
request.getSession().setAttribute(
"SPRING_SECURITY_CONTEXT",
SecurityContextHolder.getContext());

return authentication;
}

private Collection

Подробнее здесь: https://stackoverflow.com/questions/794 ... entication

Вернуться в «JAVA»