赞
踩
spring security 的核心功能主要包括:
权限管理:功能权限,访问权限,菜单权限,拦截器,过滤器等等
spring security是 Spring 项目组中用来提供安全认证服务的框架。其核心就是一组过滤器链,项目启动后将会自动配置。最核心的就是 Basic Authentication Filter 用来认证用户的身份,一个在spring security中一种过滤器处理一种认证方式。
二、
记住几个类:
Spring Security的两个主要目标是 “认证” 和 “授权”(访问控制)。
1)“认证”(Authentication)
身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。
身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。
2)“授权” (Authorization)
授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。
这个概念是通用的,而不是只在Spring Security 中存在。
认证和授权实现步骤:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
@EnableWebSecurity //开启WebSecurity模式 public class securityConfig extends WebSecurityConfigurerAdapter { //WebSecurityConfigurerAdapter自定义Security策略 //授权 //AOP思想 //链式编程 @Override protected void configure(HttpSecurity http) throws Exception { //请求授权的规则 //首页所有人可以访问,功能页只有有对应权限的人才能访问 http.authorizeRequests() //http.authorizeRequests()认证请求,.antMatchers("/")代表具体的请求, .antMatchers("/").permitAll() // .permitAll()代表所有人可以访问 .antMatchers("/level1/**").hasRole("vip1") //.hasRole("vip1")代表只有vip1权限的人才能访问 .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); //没有权限的话默认跳到登录页面,需要开启登录的页面 http.formLogin(); } //认证 springboot 2.1可以直接使用 //密码编码:Password Encoding,没被加密,不能直接使用 ,spring scurity 5.0+ 新增了很多加密方法 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) //需要连接数据库时使用jdbc的认证,jdbcAuthentication .withUser("wei").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")//用户,密码(加密),给与的权限(这里只赋予了用户访问vip2和vip3的权限) .and()//.and拼接用户 .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3") .and()//.and拼接用户 .withUser("gitee").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1"); } }
@Override
protected void configure(HttpSecurity http) throws Exception {
//....
//开启自动配置的注销的功能
// /logout 注销请求
//注销,开启注销功能,logoutSuccessUrl注销完跳到指定页(不写则默认登录页面)
http.logout().logoutSuccessUrl("/"); // http.logout().deleteCookies("remove").invalidateHttpSession(true)去除cookie以及session
}
1)导包
<!-- thymeleaf和springsecurity整合包 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
2) 我们需要结合thymeleaf中的一些功能
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
<!--登录注销--> <div class="right menu"> <!--如果未登录则显示登录按钮,否则不显示--> <div sec:authorize="!isAuthenticated()"> <!--未登录--> <a class="item" th:href="@{/toLogin}"> <i class="address card icon"></i> 登录 </a> </div> <div sec:authorize="isAuthenticated()"> <!--注销--> <a class="item" th:href="@{/logout}"> <i class="sign-out icon"></i> 注销 </a> </div> <div sec:authorize="isAuthenticated()"> <!--注销--> <a class="item"> 用户名:<span sec:authentication="name"></span> <!-- 角色:<span sec:authentication="principal.getAuthorities()"></span>--> </a> </div>
http.csrf().disable();//关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求
http.logout().logoutSuccessUrl("/");
<div class="ui three column stackable grid"> <!-- 菜单根据用户的角色动态的显示--> <div class="column" sec:authorize="hasRole('vip1')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 1</h5> <hr> <div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> Level-1-1</a></div> <div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div> <div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div> </div> </div> </div> </div> <div class="column" sec:authorize="hasRole('vip2')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 2</h5> <hr> <div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i> Level-2-1</a></div> <div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div> <div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i> Level-2-3</a></div> </div> </div> </div> </div> <div class="column" sec:authorize="hasRole('vip3')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 3</h5> <hr> <div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i> Level-3-1</a></div> <div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div> <div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> Level-3-3</a></div> </div> </div> </div> </div> </div>
1、开启记住我功能
//定制请求的授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
//本质就是保存cookie
//记住我
http.rememberMe();
}
2、我们再次启动项目测试一下,发现登录页多了一个记住我功能,我们登录之后关闭 浏览器,然后重新打开浏览器访问,发现用户依旧存在!
3.我们点击注销的时候,可以发现,spring security 帮我们自动删除了这个 cookie
4.结论:登录成功后,将cookie发送给浏览器保存,以后登录带上这个cookie,只要通过检查就可以免登录了。如果点击注销,则会删除这个cookie,具体的原理我们在JavaWeb阶段都讲过了,这里就不在多说了!
现在这个登录页面都是spring security 默认的,怎么样可以使用我们自己写的Login界面呢?
1、在登录页配置后面指定 loginpage
http.formLogin().loginPage("/toLogin");
2、然后前端也需要指向我们自己定义的 login请求
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
3.我们登录,需要将这些信息发送到哪里,我们也需要配置,login.html 配置提交请求及方式,方式必须为post:
在 loginPage()源码中的注释上有写明:
<form th:action="@{/login}" method="post"> <div class="field"> <label>Username</label> <div class="ui left icon input"> <input type="text" placeholder="Username" name="username"> <i class="user icon"></i> </div> </div> <div class="field"> <label>Password</label> <div class="ui left icon input"> <input type="password" name="password"> <i class="lock icon"></i> </div> </div> <input type="submit" class="ui blue submit button"/> </form>
4、这个请求提交上来,我们还需要验证处理,怎么做呢?我们可以查看formLogin()方法的源码!我们配置接收登录的用户名和密码的参数!
http.formLogin()
.usernameParameter("username")
.passwordParameter("password")
.loginPage("/toLogin")
.loginProcessingUrl("/login"); // 登陆表单提交请求
5、在登录页增加记住我的多选框
<input type="checkbox" name="remember"> 记住我
6、后端验证处理!
//定制记住我的参数!
http.rememberMe().rememberMeParameter("remember");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。