SpringBootSecurity安全相关

一、登录&认证&授权&权限控制&注销&记住我&定制登录页

安全部分内容主要以演示功能为主,实际项目中用户信息和权限都存在数据库中。

1.引入依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

2.编写SpringSecurity的配置类

  1. 控制请求的访问权限configure(HttpSecurity http);
  2. 定义认证规则configure(AuthenticationManagerBuilder auth);
  3. 开启自动配置的登陆功能configure(HttpSecurity http);
    1. 默认post形式的 /login代表处理登陆,get形式的/login代表进入登录页,/login?error就是登录失败,/login?logout代表注销
    2. 一但定制loginPage参数,那么 loginPage的post请求就是登陆请求,get请求就是进入登录页,定制的路径后加?error就是登录失败,定制的路径后加?logout就是注销
  4. 注销http.logout();
    1. 访问 /logout 表示用户注销,清空session
    2. 注销成功会返回 /login?logout 页面;
  5. 记住我http.rememberMe();
    1. 登陆成功以后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登录
    2. 点击注销会删除cookie
//此注解中包含@Configuration
@EnableWebSecurity//开启安全模块
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http);
        
        //定制请求的授权规则
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("VIP1")
                .antMatchers("/level2/**").hasRole("VIP2")
                .antMatchers("/level3/**").hasRole("VIP3");


        //开启自动配置的登陆功能,效果,如果没有登陆,没有权限就会来到登陆页面
        //可以指定user和pwd为页面传的用户名密码的参数名,默认是username和password
        //loginPage--自定义登录请求
        http.formLogin().usernameParameter("user").passwordParameter("pwd")
                .loginPage("/userlogin");
        
        
        //开启自动配置的注销功能。
        http.logout().logoutSuccessUrl("/");//注销成功以后来到首页/
       

        //开启记住我功能
        //可以指定页面发送的参数name,默认为remeberme
        http.rememberMe().rememberMeParameter("remeber");
       
    }

    //定义认证规则(用户名、密码、角色)
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //super.configure(auth);
        auth.inMemoryAuthentication()
                .withUser("zhangsan").password("123456").roles("VIP1","VIP2")
                .and()
                .withUser("lisi").password("123456").roles("VIP2","VIP3")
                .and()
                .withUser("wangwu").password("123456").roles("VIP1","VIP3");

    }
}

3.页面获取登录信息
引入依赖

		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-springsecurity4</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

名称空间thymeleaf-extras-springsecurity4

<html xmlns:th="http://www.thymeleaf.org"
	  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

标签sec:authentication

<div sec:authorize="isAuthenticated()">
	<h2><span sec:authentication="name"></span>,您好,您的角色有:
		<span sec:authentication="principal.authorities"></span></h2>
	<form th:action="@{/logout}" method="post">
		<input type="submit" value="注销"/>
	</form>
</div>

分权限显示hasRole()

<div sec:authorize="hasRole('VIP1')">
	<h3>普通武功秘籍</h3>
	<ul>
		<li><a th:href="@{/level1/1}">罗汉拳</a></li>
		<li><a th:href="@{/level1/2}">武当长拳</a></li>
		<li><a th:href="@{/level1/3}">全真剑法</a></li>
	</ul>
</div>

<div sec:authorize="hasRole('VIP2')">
	<h3>高级武功秘籍</h3>
	<ul>
		<li><a th:href="@{/level2/1}">太极拳</a></li>
		<li><a th:href="@{/level2/2}">七伤拳</a></li>
		<li><a th:href="@{/level2/3}">梯云纵</a></li>
	</ul>
</div>

<div sec:authorize="hasRole('VIP3')">
	<h3>绝世武功秘籍</h3>
	<ul>
		<li><a th:href="@{/level3/1}">葵花宝典</a></li>
		<li><a th:href="@{/level3/2}">龟派气功</a></li>
		<li><a th:href="@{/level3/3}">独孤九剑</a></li>
	</ul>
</div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值