SpringBoot整合SpringSecurity
一、创建项目,选择依赖
选择Spring Web、Thymeleaf即可




二、在pom文件中导入相关依赖
<!-- 导入SpringSecurity的启动器 --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId></dependency>三、在resources\templates下准备页面
目录结构如下

index.html
<!DOCTYPE html><html lang="en" level_1.html、level_2.html、level_3.html内容相同,在此不多赘述,将数字部分替换即可
<!DOCTYPE html><html lang="en" 
四、构建controller层
package cn.byuan.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class LevelAction { @RequestMapping({"/", "/index", "index.html"}) public String goToIndex(){ return "index"; }// 这里的url就是上面index.html中a标签中出现的url @RequestMapping("/level_1/gotoHtml") public String goToLevel1(){ return "level_1"; } @RequestMapping("/level_2/gotoHtml") public String goToLevel2(){ return "level_2"; } @RequestMapping("/level_3/gotoHtml") public String goToLevel3(){ return "level_3"; }}五、创建配置类,进行SpringSecurity的相关配置
SpringSecrity的两大核心:认证(Authentication)、授权(Authorization)
SpringSecurity的主要类
| 主要类 | 含义 |
|---|---|
| @EnableWebSecurity | 开启WebSecurity |
| WebSecurityConfigurerAdapter | 自定义security策略 |
| AuthenticationManagerBuilder | 自定义认证策略 |
创建配置类
package cn.byuan.config;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@EnableWebSecurity// 开启WebSecurity模块public class SecurityConfig extends WebSecurityConfigurerAdapter { }光标移入花括号内,按下 ctrl + o

package cn.byuan.config;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;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;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;@EnableWebSecurity// 开启WebSecurity模块public class SecurityConfig extends WebSecurityConfigurerAdapter { /* * 配置授权规则 * */ @Override protected void configure(HttpSecurity http) throws Exception {// 添加请求授权规则 http.authorizeRequests() .antMatchers("/").permitAll()// 首页所有人都可以访问 .antMatchers("/level_1/**").hasRole("vip1")// level_1下的所有请求, vip1用户才可以访问 .antMatchers("/level_2/**").hasRole("vip2")// level_2下的所有请求, vip2用户才可以访问 .antMatchers("/level_3/**").hasRole("vip3");// level_3下的所有请求, vip3用户才可以访问 http.formLogin();// 开启登录页面, 即无权限的话跳转到登录页面, 默认地址: /login, 这是为了有人直接访问权限范围内某一url http.logout().logoutSuccessUrl("/");// 注销后跳转到首页 http.rememberMe();// 开启记住我功能, 默认保存两周, 底层使用cookie机制实现 } /* * 配置认证规则 * * 在新版本的SpringSecurity中新增了许多加密方法, 不使用加密的话就会出现异常 * 这里我们在内存中对用户进行模拟, 真正的开发过程中会使用数据库 * * */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .passwordEncoder(new BCryptPasswordEncoder()) .withUser("root").password(new BCryptPasswordEncoder().encode......原文转载:http://www.shaoqun.com/a/862436.html
跨境电商:https://www.ikjzd.com/
萌店:https://www.ikjzd.com/w/1538
modcloth:https://www.ikjzd.com/w/1271
徐家骏:https://www.ikjzd.com/w/1803
SpringBoot整合SpringSecurity一、创建项目,选择依赖选择SpringWeb、Thymeleaf即可二、在pom文件中导入相关依赖<!--导入SpringSecurity的启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>s
tineye:https://www.ikjzd.com/w/448
亚马逊斥资28亿美元新建AWS云服务区域:https://www.ikjzd.com/articles/133412
针对Google如何通过竞争对手去挖掘关键词?:https://www.ikjzd.com/articles/133421
B2B企业如何在俄罗斯电商市场中自建搜索广告?:https://www.ikjzd.com/articles/133420
千万级操盘手分享:我是如何在黑五网一稳住Best Seller排名的?:https://www.ikjzd.com/articles/133419
我偷偷的摸了老师下面 趁老师睡着扒了她的内裤:http://lady.shaoqun.com/a/247944.html
老师抬起臀部让我进去 掀开老师湿润的短裙:http://lady.shaoqun.com/a/248339.html
老外又粗又长一晚做五次 被做爽了的细节过程:http://lady.shaoqun.com/m/a/247225.html
为什么孕中期女性合住一个房间更容易高潮?:http://lady.shaoqun.com/a/413009.html
"睡400个女生同时和17个女朋友约会":不要和渣男谈人性:http://lady.shaoqun.com/a/413010.html
我婆婆在我老婆加班的时候和我深谈男女:http://lady.shaoqun.com/a/413011.html
对于大学情侣来说,什么是最好的选择,什么是最值得的?:http://lady.shaoqun.com/a/413012.html
没有评论:
发表评论