【发布时间】:2018-01-15 03:02:41
【问题描述】:
@Service
public class UserDetService implements UserDetailsService{
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
User user = userRepository.findByUserName(userName);
if(user == null){
throw new UsernameNotFoundException(userName);
}
return new UserPrincipal(user);
}
}
SecConfig.java
@Configuration
@EnableWebSecurity
public class SecConfig extends WebSecurityConfigurerAdapter{
@Bean
public UserDetailsService userDetailsService(){
return new UserDetService();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(userDetailsService());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/hello").access("hasRole('ROLE_ADMIN')")
.anyRequest().permitAll()
.and()
.formLogin().loginPage("/login")
.usernameParameter("username").passwordParameter("password")
.and()
.logout().logoutSuccessUrl("/login?logout")
.and()
.exceptionHandling().accessDeniedPage("/403")
.and()
.csrf();
}
}
我正在学习 Spring Security,但没有人问我是否正确,这是为了确保登录身份验证安全还是我错过了什么?
我还感到困惑,我没有编写任何查询来获取用户名和密码,如何从用户输入和数据库验证用户名和密码以及验证发生在哪里?
感谢这里有这么多乐于助人的人
【问题讨论】:
标签: spring-boot jdbc spring-security