我的情况是,我有一个采用 Collection 的方法,当我尝试将列表值添加到集合参数中时,我将该值作为列表,但它不起作用,所以我应该转换它吗?
更新: 当我尝试创建时:
new org.springframework.security.core.userdetails.User(
user.getEmail(), user.getPassword(), true, true, true, true,
user.getAuthorities());
Run Code Online (Sandbox Code Playgroud)
其中 user.getAuthorities() 返回我定义的 Authority 对象的数组列表
上面的代码给出了构造函数未定义的错误,当我添加集合转换时它工作正常。
org.springframework.security.core.userdetails
类用户构造函数
User(java.lang.String username, java.lang.String password,
boolean enabled, boolean accountNonExpired,
boolean credentialsNonExpired, boolean accountNonLocked,
java.util.Collection<? extends GrantedAuthority> authorities)
Run Code Online (Sandbox Code Playgroud) 我正在尝试加载一些基本的用户信息,这些信息应该可用于每个 HTTP 请求。我读到我可以使用带有 @ModelAttribute 方法的 @ControllerAdvice 类来做到这一点。例如:
@ControllerAdvice
public class DefaultController{
@ModelAttribute
public void load(ModelMap model){
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法访问由 Spring Security 管理的 Principal 对象。我尝试了以下方法:
@ModelAttribute
public void load(ModelMap model, @AuthenticationPrincipal CustomUser user){
}
Run Code Online (Sandbox Code Playgroud)
和
@ModelAttribute
public void load(ModelMap model, Principal user){
//Cast to CustomUser object
}
Run Code Online (Sandbox Code Playgroud)
但是这两种方法都不起作用。我为用户对象获取空值。我想知道是否有人知道如何在 @ModelAttribute 方法中获取 Principal 对象
我该如何进行加密。这样在数据库中它就不会显示用户密码。我现在保存在数据库中 - 登录名和密码,用户角色。我需要密码必须在数据库中加密(BCrypt)
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.antMatchers("/**").permitAll()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/allStudents")
.and()
.logout()
.and()
.csrf().disable();
}
@Bean
public PasswordEncoder weDoNotWantEncryption() {
return new PasswordEncoder() {
@Override
public String encode(CharSequence rawPassword) {
return rawPassword.toString();
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
return rawPassword.toString().equals(encodedPassword);
}
};
}
}
Run Code Online (Sandbox Code Playgroud) 我正在现有的Web应用程序中实现Spring Security,当我尝试登录时,我尝试查看日志时收到的响应是错误的凭据(即使凭据正确)(即使凭据正确)
WARN osscbcrypt.BCryptPasswordEncoder-空的编码密码
这是凭证不当的原因吗?
这是我的代码
@PostConstruct
public void init() {
try {
authenticationManagerBuilder
.userDetailsService(accountDetailsService)
.passwordEncoder(passwordEncoder());
} catch (Exception e) {
throw new BeanInitializationException("Security configuration failed", e);
}
}
@Bean
public PasswordEncoder passwordEncoder()
{
return new BCryptPasswordEncoder();
}
Run Code Online (Sandbox Code Playgroud)
提前致谢 !!
我需要为 LoginI18n loginView 忘记密码按钮实现点击侦听器。谁能帮我吗?
我尝试在 AbstractLogin 类中寻找 ForgotPasswordEvent 方法,但未能使用它。