пятница, 24 октября 2008 г.

Программная аутентификация в Spring Security 2.0

Порой возникает необходимость произвести аутентификацию в приложении программно, а не через POST-запрос. В Spring Security это можно сделать с помощью, буквально, трех строк кода:

@Autowired
private AuthenticationManager _authenticationManager;

private void authenticate(HttpServletRequest request, String nick, String password) {
Authentication auth = new UsernamePasswordAuthenticationToken(nick, password);
WebAuthenticationDetails details = new WebAuthenticationDetails(request);
auth.setDetails(details);
Authentication fullauth = _authenticationManager.authenticate(auth);
SecurityContextHolder.getContext().setAuthentication(fullauth);
}

1 комментарий:

Анонимный комментирует...

Спасибо, это то, что мне было нужно.