Bom dia prezados,
Estou desenvolvendo autenticação de Usuário com Spring Security.
Eu preciso validar o login com facesmessage, mas não funciona no authenticationsuccesshandler e authenticationfailurehandler, como posso fazer essa implementação? Hoje está assim:
.xml :
<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http>
<intercept-url pattern="/admin/**" access="ROLE_SUPER_ADMIN" />
<form-login login-page="/index.xhtml"
always-use-default-target="false"
authentication-success-handler-ref="authenticationSuccessHandler"
authentication-failure-url="/index.xhtml" />
<logout logout-success-url="/index.xhtml"/>
</http>
<b:bean id="authenticationSuccessHandler"
class="br.com.ribas.utils.security.UrlAuthenticationSuccessHandler" />
<b:bean id="authenticationFailureHandler"
class="br.com.ribas.utils.security.UrlAuthenticationFailureHandler" />
<authentication-manager>
<authentication-provider>
<password-encoder hash="md5"/>
<jdbc-user-service data-source-ref="myDataSource"
authorities-by-username-query="SELECT u.login, p.permission
FROM user_ofoka u, user_permission p
WHERE u.id = p.id
AND u.login = ?"
users-by-username-query="SELECT login, password, ativo
FROM user_ofoka
WHERE login = ? " />
</authentication-provider>
</authentication-manager>
</b:beans>
Autenticação:
public class UrlAuthenticationSuccessHandler implements
AuthenticationSuccessHandler {
protected final Log logger = LogFactory.getLog(this.getClass());
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
protected UrlAuthenticationSuccessHandler() {
super();
}
// API
@Override
public void onAuthenticationSuccess(final HttpServletRequest request,
final HttpServletResponse response,
final Authentication authentication) throws IOException {
System.out.println("Chamou onAuthenticationSuccess !!!");
handle(request, response, authentication);
clearAuthenticationAttributes(request);
System.out.println("Finalizou onAuthenticationSuccess !!!");
}
// IMPL
protected void handle(final HttpServletRequest request, final HttpServletResponse response,
final Authentication authentication) throws IOException {
final String targetUrl = determineTargetUrl(authentication);
System.out.println("Chamou pagina: " + targetUrl);
if (response.isCommitted()) {
logger.debug("Response já foi confirmada . Não é possível redirecionar para " + targetUrl);
return;
}
redirectStrategy.sendRedirect(request, response, targetUrl);
}
protected String determineTargetUrl(final Authentication authentication) {
boolean isCommon = false;
boolean isAdmin = false;
boolean isSuperAdmin = false;
final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
for (final GrantedAuthority grantedAuthority : authorities) {
if (grantedAuthority.getAuthority().equals("ROLE_ALUNO")) {
isCommon = true;
break;
} else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
isAdmin = true;
break;
} else if(grantedAuthority.getAuthority().equals("ROLE_SUPER_ADMIN")){
isSuperAdmin = true;
break;
}
}
System.out.println("Entrou if da página");
if (isCommon) {
return "/admin/inc/header.xhtml";
} else if (isAdmin) {
return "/admin/inc/header.xhtml";
} else if(isSuperAdmin){
return "/admin/inc/header.xhtml";
} else {
System.out.println("ERRO: ");
throw new IllegalStateException();
}
}
/**
* Remove qualquer autenticação que estava antes armazenada na sessão.
*/
protected final void clearAuthenticationAttributes(final HttpServletRequest request) {
final HttpSession session = request.getSession(false);
System.out.println("Chamou pagina clearAuthenticationAttributes !!!");
if (session == null) {
return;
}
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
}
public void setRedirectStrategy(final RedirectStrategy redirectStrategy) {
this.redirectStrategy = redirectStrategy;
}
protected RedirectStrategy getRedirectStrategy() {
return redirectStrategy;
}
}