spring Security安全框架
基于springBoot框架
spring Security能做什么
1.认证,实现登录安全功能
2.授权,确认在当前系统下用户所拥有的功能权限
spring Security如何运行
1.在pom.xml中引入spring-boot-starter-security
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
效果
在浏览器中输入 http://localhost:8080 会自动跳到spring Security的登录页面,username默认为user,password为后台给你的密码 如下代码;默认拦截所有的url
2020-02-11 12:42:32.656 INFO 1084 --- [ restartedMain] .s.s.UserDetailsServiceAutoConfiguration :
Using generated security password: 5c34ca30-49e4-4bd8-9db7-6d69b4bd0582
spring Security的原理
通过一系列的filter来保护web资源;对API接口则使用AOP来保护
每个Filter所代表的含义
spring Security的配置
1.开启过滤器支持
只需要让自己的类继承AbstractSecurityWebApplicationInitializer
2.配置
让自己的类继承WebSecurityConfigurerAdapter
3.用户认证
我们通过重写protected void configure(AuthenticationManagerBuilder auth)方法来实现定制;这个方法在继承了WebSecurityConfigurerAdapter类的配置类中
3.1内存中的用户
通过inMemoryAuthentication方法来添加
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//userDetailsService(userDetailsService) 认证所采用的方式
//passwordEncoder(passwordEncoder()) 加密解密所采用的方式
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
auth.inMemoryAuthentication()
.withUser("wyf").password("wyf").roles("ROLE_ADMIN");
.and()
.withUser("wyf").password("wyf").roles("ROLE_USER");
}
3.2通用的用户
需要自定义实现UserDetailsService接口
public class userDetailsServiceImpl implements UserDetailsService {
@Autowired
private userServerce userServerce;
@Autowired
private permissionDao permissionDao;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
user user = userServerce.getuser(username);
if(user == null){
throw new AuthenticationCredentialsNotFoundException("用户名不存在");
}else if (user.getStatus() == com.mannnger.model.user.Status.DISABLED){
throw new LockedException("用户被锁定,请联系管理员");
}
loginUserDto loginUserDto = new loginUserDto();
BeanUtils.copyProperties(user,loginUserDto);
loginUserDto.setPermissions(permissionDao.listByUserId(user.getId().longValue()));
System.out.println(permissionDao.listByUserId(user.getId().longValue()));
return loginUserDto;
}
}
然后在配置类中引入自定义实现的UserDetailsService接口*
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//userDetailsService(userDetailsService) 认证所采用的方式
//passwordEncoder(passwordEncoder()) 加密解密所采用的方式
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
4.请求授权
我们通过重写protected void configure(HttpSecurity http)方法来实现
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()//1
.antMatchers("/login.html","/user/add")//2
.permitAll()//3
.anyRequest()//4
.authenticated()//5;
}
1:通过authorizeRequests方法来开始请求权限配置
2.请求匹配的资源
3.用户可任意访问
4.匹配所有请求路径,除去前面已匹配过的
5.用户登录后可访问
5.定制登录行为
protected void configure(HttpSecurity http) throws Exception {
http.formLogin() //1
.loginPage("/login.html")//指定登录的html页面
.loginProcessingUrl("/login")//指定的处理路径
.successHandler(myAuthenticationSuccessHander)//2
.defaultSuccessUrl("/index");
.failureHandler(myAuthenctiationFailureHandler)//3
//退出处理
.and().logout()//4
.permitAll()
.invalidateHttpSession(true)//注销session
.deleteCookies("JSESSIONID")
.logoutSuccessHandler(myLogoutSuccessHander);//5
1.通过formLogin方法定制登录操作
2.定制登录成功后的处理类
defaultSuccessUrl("/index");指定登录后转向的页面
3.定制登录失败后的处理类
4.使用logout方法定制注销行为
5.指定注销成功后转向的页面