SpringBoot集成Spring Security(授权与认证)

Stella981
• 阅读 511

⒈添加starter依赖

 1         <dependency>
 2             <groupId>org.springframework.boot</groupId>
 3             <artifactId>spring-boot-starter-web</artifactId>
 4         </dependency>
 5 
 6         <dependency>
 7             <groupId>org.springframework.boot</groupId>
 8             <artifactId>spring-boot-starter-security</artifactId>
 9         </dependency>
10 
11         <dependency>
12             <groupId>org.springframework.boot</groupId>
13             <artifactId>spring-boot-starter-thymeleaf</artifactId>
14         </dependency>
15 
16         <!--添加Thymeleaf Spring Security依赖-->
17         <dependency>
18             <groupId>org.thymeleaf.extras</groupId>
19             <artifactId>thymeleaf-extras-springsecurity4</artifactId>
20             <version>3.0.4.RELEASE</version>
21         </dependency>

⒉使用配置类定义授权与定义规则

 1 package cn.coreqi.config;
 2 
 3 import org.springframework.context.annotation.Configuration;
 4 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
 5 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 6 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 7 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 8 
 9 //@Configuration
10 @EnableWebSecurity
11 public class SecurityConfig extends WebSecurityConfigurerAdapter {
12 
13     //定义授权规则
14     @Override
15     protected void configure(HttpSecurity http) throws Exception {
16         //定制请求授权规则
17         http.authorizeRequests()
18                 .antMatchers("/css/**","/js/**","/fonts/**","index").permitAll()    //不拦截,直接访问
19                 .antMatchers("/vip1/**").hasRole("VIP1")
20                 .antMatchers("/vip2/**").hasRole("VIP2")
21                 .antMatchers("/vip3/**").hasRole("VIP3");
22         //开启登陆功能(自动配置)
23         //如果没有登陆就会来到/login(自动生成)登陆页面
24         //如果登陆失败就会重定向到/login?error
25         //默认post形式的/login代表处理登陆
26         http.formLogin().loginPage("/userLogin").failureUrl("/login-error");
27         //开启自动配置的注销功能
28         //访问/logout表示用户注销,清空session
29         //注销成功会返回/login?logout页面
30         //logoutSuccessUrl()设置注销成功后跳转的页面地址
31         http.logout().logoutSuccessUrl("/");
32         //开启记住我功能
33         //登陆成功以后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登陆
34         //点击注销会删除cookie
35         http.rememberMe();
36     }
37 
38     //定义认证规则
39     @Override
40     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
41         //jdbcAuthentication()  在JDBC中查找用户
42         //inMemoryAuthentication() 在内存中查找用户
43 
44         auth.inMemoryAuthentication().withUser("fanqi").password("admin").roles("VIP1","VIP2","VIP3")
45                 .and()
46                 .withUser("zhangsan").password("123456").roles("VIP1");
47     }
48 }

⒊编写控制器类(略)

⒋编写相关页面

 1 <!DOCTYPE html>
 2 <html lang="en"
 3       xmlns:th="http://www.thymeleaf.org"
 4       xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
 5       xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
 6 <head>
 7     <meta charset="UTF-8">
 8     <title>登录页面</title>
 9 </head>
10 <body>
11     <div sec:authorize="isAuthenticated()">
12         <p>用户已登录</p>
13         <p>登录的用户名为:<span sec:authentication="name"></span></p>
14         <p>用户角色为:<span sec:authentication="principal.authorities"></span></p>
15     </div>
16     <div sec:authorize="isAnonymous()">
17         <p>用户未登录</p>
18     </div>
19 </body>
20 </html>
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Easter79 Easter79
2年前
SpringBoot集成Spring Security(授权与认证)
⒈添加starter依赖1<dependency2<groupIdorg.springframework.boot</groupId3<artifactIdspringbootstarterweb</artifactId4
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这