Boa tarde estou seguindo um tutorial de como fazer uma página de login com spring security e jsf, nom meu bean tenho o seguinte código:
private Usuario usuario;
public UsuarioBean() {
usuario = new Usuario();
SecurityContext context = SecurityContextHolder.getContext();
if (context instanceof SecurityContext){
Authentication authentication = context.getAuthentication();
if (authentication instanceof Authentication){
usuario.setUsername(((User) authentication.getPrincipal()).getUsername()); //erro aqui.
}
}
}
ao iniciar o projeto sempre ocorre esse erro: java.lang.String cannot be cast to org.springframework.security.core.userdetails.User
o que poderia ser?
Meu applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:b="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/inicio/inicio.xhtml" access="hasRole('ROLE_USER')" />
<form-login login-page="/login.xhtml" authentication-failure-url="/login.xhtml?erro=true" />
</http>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="SELECT username, password, enable FROM usuario WHERE username=?"
authorities-by-username-query="SELECT Usuario_username as username, autorizacoes_nome as authority FROM usuario_autorizacao WHERE Usuario_username=?" />
</authentication-provider>
</authentication-manager>
<b:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<b:property name="url" value="jdbc:mysql://localhost:3306/database" />
<b:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<b:property name="username" value="root" />
<b:property name="password" value="senha" />
</b:bean>
</b:beans>
Entidade Usuario:
@Entity
@Table(name = "USUARIO")
public class Usuario implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String username;
private String password;
@Column(name = "enable", columnDefinition = "BOOLEAN")
private boolean enable;
@OneToMany
private List<Autorizacao> autorizacoes;
//gets e sets
Entidade Autorizacao:
@Entity
@Table(name = "AUTORIZACAO")
public class Autorizacao implements Serializable{
private static final long serialVersionUID = 1L;
@Id
private String nome;
//gets e sets