【发布时间】:2020-01-18 09:20:18
【问题描述】:
我是 Spring Security 的新手,想对拥有 userId 和密码的用户进行身份验证。 userId 在注册时提供给用户。
第二个问题是用户名和密码在不同的表中。 那么,如何自定义 Spring Security 以满足这两个要求。
这是我的安全配置类。 我正在使用弹簧靴 2
公共类 SecurityConfig 扩展 WebSecurityConfigurerAdapter {
@Autowired
private DbConfig dbConfig;
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/static/**")
.antMatchers(HttpMethod.GET, "/public/**")
.antMatchers(HttpMethod.GET, "/index.html");
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dbConfig.dataSource())
.usersByUsernameQuery("select username,password,active from user where username=?")
.authoritiesByUsernameQuery("select username,authority from authorities where username=?")
.passwordEncoder(passwordEncoder());
}
private PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login")
.permitAll()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/dashboard")
.and()
.csrf()
.disable();
}
}
【问题讨论】:
标签: java spring spring-boot spring-mvc spring-security