我对如何在会话中获取用户名感到怀疑。我使用的Spring Security 4.2在会话中获取用户名Spring安全
我有我的课Usuario
@Entity
@Data
public class Usuario {
@Id @GeneratedValue
private Integer id;
private String login;
private String senha;
private String papel;
}
我班UsuarioController
@Named
@ViewScoped
public class UsuarioController {
@Autowired
private UsuarioRepository usuarioRepository;
@Getter @Setter
private List<Usuario> usuarios;
@Getter @Setter
private Usuario usuario = new Usuario();
}
而且我的课SecurityConfig,起到过滤器的作用,已建成为春季安全。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UsuarioRepository usuarioRepository;
@Override
protected void configure(HttpSecurity http) {
try {
http.csrf().disable();
http
.userDetailsService(userDetailsService())
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/cliente.jsf").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login.jsf")
.permitAll()
.failureUrl("/login.jsf?error=true")
.defaultSuccessUrl("/cliente.jsf")
.and()
.logout()
.logoutSuccessUrl("/login.jsf");
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
protected UserDetailsService userDetailsService() {
List<Usuario> usuarios = usuarioRepository.findAll();
List<UserDetails> users = new ArrayList<>();
for(Usuario u: usuarios){
UserDetails user = new User(u.getLogin(), u.getSenha(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_"+u.getPapel()));
users.add(user);
} return new InMemoryUserDetailsManager(users);
}
}
我已经研究过论坛上的其他帖子,没有帮助,有什么建议吗?我是否需要创建另一个课程?
对于CDI/JSF您使用了错误的组合'@ Named'和'@ Viewsoped'。一个是'CDI',另一个是'JSF'。如果它以这种方式工作,这个问题决不是与jsf相关的。 – Kukeltje
嗨Kukeltje,什么是正确的形式? – Genn
通常我会说,尝试谷歌,但我心情很好:https://stackoverflow.com/questions/14384369/how-to-replace-managedbean-viewscope-by-cdi-in-jsf-2 -0-2-1 – Kukeltje