My project is JSF with Spring security.
I logged success with spring security and redirect my home, but is still anonymous. I user security tags and does not work. My class UserDetailsService:
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UsuarioDetails usuario = userMapper.findByUsername(username);
if (Objects.isNull(usuario)) throw new UsernameNotFoundException(username);
return usuario;
}
and
protected void configure(HttpSecurity http) {
try {
List<Transacao> transacoes = transacaoDao.findAll();
http.csrf().disable();
http.authorizeRequests().antMatchers("/", "/index.xhtml", "/javax.faces.resource/**").permitAll();
http.authorizeRequests().anyRequest().authenticated()
.and()
.formLogin().loginPage("/template/login.xhtml").usernameParameter("username").passwordParameter("password")
.permitAll()
.failureUrl("/template/login.jsf?error=true").defaultSuccessUrl("/index.xhtml")
.and().exceptionHandling().accessDeniedPage("/403.xhtml")
.and().logout().logoutSuccessUrl("/login.xhtml").invalidateHttpSession(true).deleteCookies("JSESSIONID")
.and().httpBasic()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);;
for(Transacao t : transacoes) {
http.authorizeRequests().antMatchers(t.getUrl()).access(t.getNome());
}
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
My UserDetails have username, password and roles. When the login redirect to home page, in my controller i inpected:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
The Authentication is anonymusUser. And a user um my home page tags don't show elements:
<sec:authorize access="hasRole('PROCESSO')">
<h:outputLabel value="Show my"></h:outputLabel>
</sec:authorize>