Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Erro quando adiciono a JSTL do Spring Security

Quando eu tento adicionar a opção

<security:authentication property="principal.username"/>

na minha home ele começa a retornar o seguinte erro

HTTP Status 500 - javax.servlet.ServletException: javax.servlet.jsp.JspException: org.springframework.beans.NotReadablePropertyException: Invalid property 'principal.username' of bean class [org.springframework.security.authentication.AnonymousAuthenticationToken]: Bean property 'principal.username' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

Eu já verifiquei minhas classes Usuario e UsuarioDAO

package br.com.cdc.loja.models;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Lnk on 29/12/2015.
 */
@Entity
public class Usuario implements UserDetails{

    @Id
    private String user;

    private String pass;

    @OneToMany(fetch = FetchType.EAGER)
    private List<Role> roles = new ArrayList<Role>();

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.roles;
    }

    @Override
    public String getPassword() {
        return this.pass;
    }

    @Override
    public String getUsername() {
        return this.user;
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}

e a UsuarioDAO:

package br.com.cdc.loja.daos;

import br.com.cdc.loja.models.Usuario;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Repository
public class UsuarioDAO implements UserDetailsService {

    @PersistenceContext
    private EntityManager manager;

    @Override
    public UserDetails loadUserByUsername(String user) throws UsernameNotFoundException {
        List<Usuario> usuarios = manager.createQuery("select u from Usuario u where u.user = :user", Usuario.class)
                .setParameter("user", user).getResultList();

        if (usuarios.isEmpty()) {
            throw new UsernameNotFoundException("Usuario " + user + " não encontrado");
        }

        return usuarios.get(0);
    }
}

Mas mesmo assim ele não consegue acessar a propriedade username.

1 resposta
solução!

Eu arrumei uma forma de solucionar estre problema.

O que estava acontecendo. A Tag <security:authentication property="principal.username"/> só estava funcionando caso tivesse algum usuario logado.

Então eu alterei minha JSP para só mostrar a tag caso tivesse algum usuario logado através do código.

  <ul class="nav navbar-nav navbar-right">
                <c:if test="${auth}">
                    <li>
                        <sec:authentication property="principal.username"/>
                    </li>
                </c:if>
                <c:if test="${not auth}">
                    <li>
                        <a href="/login">Login</a>
                    </li>
                </c:if>
            </ul>