![](/img/trans.png)
[英]Spring Security login with UserDetailsService and Java config
[英]Spring Security/Java config - Can not login with existing user
我在創建簡單的登錄表單時正在學習Spring Security 。 我正在使用Java配置。 我有內存用戶和一個簡單的過濾器鏈。
但是,當我輸入現有的用戶名和密碼組合時, Spring會將我重定向到帶有url的登錄表單: login?error.
這是我的Spring Security配置類:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// @Autowired
// AuthProvider provider;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password("").roles("USER")
.and()
.withUser("user2").password("").roles("USER")
.and()
.withUser("admin").password("1").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll();
}
// @Override
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// auth.authenticationProvider(provider);
// }
}
這是我的JSP表格:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
${message}
<br>
<form method="post" action="/login">
<input type="text" name="login"/>
<input type="text" name="pass"/>
<input type="submit" value="enter"/>
</form>
</body>
</html>
在你的代碼中
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password("").roles("USER")
.and()
.withUser("user2").password("").roles("USER")
.and()
.withUser("admin").password("1").roles("ADMIN");
}
用@Override
替換@Autowired
並在此處[1]遵循此做法: https : //www.baeldung.com/spring-security-login
... auth.inMemoryAuthentication()
.withUser("user1").password(passwordEncoder().encode("user1Pass")).roles("USER")
.and() ...
如下使用BCryptPasswordEncoder
作為相同代碼中的bean
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
對於管理員用戶,請嘗試:
.withUser("admin").password("{noop}1").roles("ADMIN");
這是一種以純文本形式存儲密碼的方法(顯然不建議這樣做...)。 通過添加{noop}
前綴,表明您要使用NoopPasswordEncoder
。
否則,您應該指定密碼編碼器,例如:
@Bean
public BCryptPasswordEncoder passEncoder() {
return new BCryptPasswordEncoder();
}
並像這樣更新您的SecurityConfig
:
@Autowired
private BCryptPasswordEncoder passEncoder;
@Override
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passEncoder)
.withUser("user1").password("").roles("USER")
.and()
.withUser("user2").password("").roles("USER")
.and()
.withUser("admin").password("1").roles("ADMIN");
}
看一下formLogin(String loginPage)
javadoc-username和password的默認參數是username
和password
。 因此,您應該在.jsp中引用它們,然后登錄應該起作用。 因此,您應該嘗試將jsp重構為以下內容:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
${message}
<br>
<form method="post" action="/login">
<input type="text" id="username" name="username"/>
<input type="password" id="password" name="password"/>
<input type="submit" value="enter"/>
</form>
</body>
</html>
如上所述,我的代碼中出現了所有三個錯誤。 我接受了所有三個解決方案,並且對我有用。 謝謝!
工作的代碼:
我的SecurityConfig:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password(passwordEncoder().encode("1")).roles("USER")
.and()
.withUser("user2").password(passwordEncoder().encode("1")).roles("USER")
.and()
.withUser("admin").password(passwordEncoder().encode("1")).roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("login")
.passwordParameter("pass")
.permitAll();
}
}
我的jsp表格:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
${message}
<br>
<form method="post" action="/login">
<input type="text" name="login"/>
<input type="text" name="pass"/>
<input type="submit" value="enter"/>
</form>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.