springboot學習總結(八)Spring security配置
阿新 • • 發佈:2019-04-20
class turn tor light 重寫 name brush 學習 authent
(一)配置類
Spring security的配置和Spring MVC的配置類似,只需在一個配置類上註解@EnableWebSecurity(Springboot項目可以不用),並讓這個類繼承WebSecurityConfigurerAdapter。
@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { super.configure(auth); } @Override protected void configure(HttpSecurity http) throws Exception { super.configure(http); } @Override public void configure(WebSecurity web) throws Exception { super.configure(web); } }
(二)用戶認證
通過實現UserDetailsService來實現
@Service public class CustomUserService implements UserDetailsService { @Autowired SysUserRepository sysUserRepository; @Override public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { SysUser user = sysUserRepository.findByUsername(s); if (user == null) { throw new UsernameNotFoundException("用戶名不存在"); } return user; } }
(三)請求&授權
Spring security通過重寫
protected void configure(HttpSecurity http)
這個方法來實現請求攔截的。
(四)定制登錄行為
通過重寫
protected void configure(HttpSecurity http)
這個方法(和上面是同一個方法)來定制登錄行為
springboot學習總結(八)Spring security配置