在springboot 项目中配置SpringbootSecurity 在数据库中验证
<!-- spring security 安 全 框 架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
//依赖
</dependency>
新建配置文件WebSecurityConfig
此配置类必须继承 WebSecurityConfigureAdapter
重写里面的方法
1、configure(http) 配置路由拦截策略 表单登录规则 注册规则 认证成功逻辑 ,认证失败逻辑。
2、configure (web) 配置文件无需认证路径
@Resource
private AuthenticationSuccessHandler loginSuccessHandler; //认证成功结果处理器
@Resource
private AuthenticationFailureHandler loginFailureHandler; //认证失败结果处理器
//http请求拦截配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().disable();//开启运行iframe嵌套页面
http//1、配置权限认证
.authorizeRequests()
//配置不拦截路由
.antMatchers("/500").permitAll()
.antMatchers("/403").permitAll()
.antMatchers("/404").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/**").permitAll()
.antMatchers("/WXApplets/**").permitAll() //该接口下全放开
.antMatchers("/getImage").permitAll() //放行图片验证码路径
.anyRequest() //任何其它请求
.authenticated() //都需要身份认证
.and()
//2、登录配置表单认证方式
.formLogin()
.loginPage("/login")//自定义登录页面的url
【 与登录页面的表单name 保持一致】
.usernameParameter("userno")//设置登录账号参数,与表单参数一致
.passwordParameter("password")//设置登录密码参数,与表单参数一致
//告诉Spring Security在发送指定路径时处理提交的凭证,默认情况下,将用户重定向回页面用户来自。它不会将请求传递给Spring MVC和您的控制器
//登录表单form中action的地址,也就是处理认证请求的路径,只要保持表单中action和HttpSecurity里配置的loginProcessingUrl一致就可以了,也不用自己去处理。
.loginProcessingUrl("/user/login")//配置默认登录入口
.successHandler(loginSuccessHandler)//使用自定义的成功结果处理器
.failureHandler(loginFailureHandler)//使用自定义失败的结果处理器
.and()
//3、注销
.logout()
.logoutUrl("/logout")
.logoutSuccessHandler(new CustomLogoutSuccessHandler())
.permitAll()
.and()
//4、session管理
.sessionManagement()
.invalidSessionUrl("/login") //失效后跳转到登陆页面
//单用户登录,如果有一个登录了,同一个用户在其他地方登录将前一个剔除下线
//.maximumSessions(1).expiredSessionStrategy(expiredSessionStrategy())
//单用户登录,如果有一个登录了,同一个用户在其他地方不能登录
//.maximumSessions(1).maxSessionsPreventsLogin(true) ;
.and()
//5、禁用跨站csrf攻击防御
.csrf().disable();//禁用csrf,否则post方法无法访问
}
//授权认证配置
/* @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
*//**
* 基于内存的方式,创建两个用户admin/123456,user/123456
* *//*
auth.inMemoryAuthentication()
.withUser("admin")//用户名
.password(passwordEncoder().encode("123456"))//密码
.roles("ADMIN");//角色
auth.inMemoryAuthentication()
.withUser("user")//用户名
.password(passwordEncoder().encode("123456"))//密码
.roles("USER");//角色
}*/
@Override
public void configure(WebSecurity web) throws Exception {
//配置静态文件不需要认证
web.ignoring().antMatchers("/static/**");
}
/**
* 指定加密方式
*/
@Bean
public PasswordEncoder passwordEncoder(){
// 使用BCrypt加密密码
return new BCryptPasswordEncoder();
}
将此类加上注解 @Configuration 声明这是配置类
@EnableWebSecurity 开启Spring Security
@EnableGlobalMethodSecurity(prePostEnabled = true)
开启全局配置springboot security 开启后可在接口上加入
@PerAuthorize ,@PostAuthorize 等注解,设置为TRUE
会拦截加入了这些注释的接口,去认证当前用户是否有权限访问此接口
2、自定义用户认证
@Component 加入注解 加入IOC容器
自定义一个配置类 实现UserDetailsService 这个接口
重写loadUserByUsername方法放回UserDetails
根据实际情况注入对象
1,用户基本信息对象
2,passwordEncoder //springsecurity 中提供的认证密码对象
对于权限可新建集合并向集合中加入用户权限` List authorities = new ArrayList<>();
最后使用userdetails.User对象去认证用户密和密码【注意密码需要加密】
org.springframework.security.core.userdetails.User userDetails = new org.springframework.security.core.userdetails.User(userInfo.getUserno(),passwordEncoder.encode(userInfo.getPassword()),authorities);
return userDetails;
创建认证成功处理器
1、继承SaveRequestAwareAuthenticationSuccessHandler类
重写onAuthenticationSuccess 方法
失败处理器类似、 注销处理器失败成功类似