SpringSecurity是什么
Spring 是一个非常流行和成功的 Java 应用开发框架。
Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。一般来说,Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分。与所有Spring项目一样,Spring安全性的真正强大之处在于它可以轻松地扩展以满足定制需求。这是一个权限框架。
在用户认证方面,Spring Security 框架支持主流的认证方式,包括 HTTP 基本认证、HTTP 表单验证、HTTP 摘要认证、OpenID 和 LDAP 等。在用户授权方面,Spring Security 提供了基于角色的访问控制和访问控制列表(Access Control List,ACL),可以对应用中的领域对象进行细粒度的控制。
当下市面上解决此问题存在比较有名的框架:Shiro,Spring Security
官方地址:https://spring.io/projects/spring-security#learn
SpringSecurity关键知识
Spring Security的两个主要目标是 “认证” 和 “授权”(访问控制)。
认证(Authentication)C
身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。
身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。
授权(Authorization)Z
授权发生在系统成功验证您之后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。
这个概念是通用的,在认证授权框架中存在。
如何使用SpringSecurity
1、在pom.xml中导入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、写几个用于测试的controller跳转和静态资源
静态资源
Controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RouterController {
@RequestMapping({"/","/index"})
public String index(){
return "index";
}
@RequestMapping("/toLogin")
public String toLogin(){
return "views/login";
}
@RequestMapping("/level1/{id}")
public String toLevelOne(@PathVariable("id")int id){
return "views/level1/" +id;
}
@RequestMapping("/level2/{id}")
public String toLevelTwo(@PathVariable("id")int id){
return "views/level2/" +id;
}
@RequestMapping("/level3/{id}")
public String toLevelThree(@PathVariable("id")int id){
return "views/level3/" +id;
}
}
3、创造自己的config配置类
继承方法
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity // 开启WebSecurity模式
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
}
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
}
}
4、设置授权
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页可访问,功能页权限设置
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasAnyRole("vip1")
.antMatchers("/level2/**").hasAnyRole("vip2")
.antMatchers("/level3/**").hasAnyRole("vip3");
http.formLogin() //没权限跳转去登录页面
.loginPage("/toLogin") //定制登录页替换默认页
.usernameParameter("username") //定制成功跳转和路径
.passwordParameter("password")
.loginProcessingUrl("/login");
//开启注销
http.logout().logoutSuccessUrl("/")
.logoutSuccessUrl("/mylogout").deleteCookies("remove").invalidateHttpSession(true);
//记住我 定制参数
http.rememberMe().rememberMeParameter("remember");
//关闭csrf,防止跨站攻击
http.csrf().disable();
}
5、设置认证
//认证
//springboot 2.1以下可以直接用,之后版本需要PassWordEncoder密码编译
//spring security 官方推荐的是使用bcrypt加密方式。
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
6、运行测试。分别在,不登录,登录root和登录guest下访问感受权限拦截