spriingboot -- spring-security 对页面资源的认证与授权

参考于和感谢此博主:https://www.cnblogs.com/zimug/p/11870861.html

1、引入依赖和版本:

1)、properties

	<properties>
		<java.version>1.8</java.version>
		<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
		<!-- 布局功能的支持程序  thymeleaf3主程序  layout2以上版本 --> 
		<!-- thymeleaf2   layout1--> 
		<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
	</properties>
	
	

2)、依赖

	<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
		<dependency>
		    <groupId>org.thymeleaf.extras</groupId>
		    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
		    <version>3.0.2.RELEASE</version>
		</dependency>
	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

2、配置 WebSecurityConfigurerAdapter 的相关属性:

package com.example.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;

@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter{

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		
		http.authorizeRequests().antMatchers("/").permitAll() //访问任何资源都转跳到该路径,即首页
			.antMatchers("/level1/**").hasRole("VIP1")  //访问 **/level1/**路径下的资源都需要角色 VIP1, 相当于session一样; 如 page/level1/1
			.antMatchers("/level2/**").hasRole("VIP2")
			.antMatchers("/level3/**").hasRole("VIP3");
		
//		http.formLogin();  //当要跳转需要权限的以上资源或者"/login"时会跳转到默认登录界面
		http.csrf().disable(); //将csrf关闭
		http.formLogin().loginPage("/MyLogin"); //跳转到自己制定的登陆页面
		http.formLogin().defaultSuccessUrl("/");  //登录成功跳转的页面
		http.formLogin().failureUrl("/aaa"); //登录失败后跳转到自己制定的登陆失败页面
		http.formLogin().loginProcessingUrl("/message"); //设置自定义页面form表单action路径
		
		/*
		 * 1、用户注销, 清空session
		 * 2、注销成功会返回/login?logout 页面(默认)
		 */
		http.logout().logoutSuccessUrl("/"); //设置注销成功后返回的页面, 即首页
		
		//开启 记住我, 并设置 checkbox 的 name, 向浏览器提交一个 cookie 
		http.rememberMe().rememberMeParameter("remember");

		System.out.println("WebSecurity设置成功...");
	}

	//授权角色信息
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		
		//将三个用户信息保存在内存中
		auth.inMemoryAuthentication().withUser("huang").password("admin").roles("VIP1", "VIP2")
									.and().withUser("he").password("admin").roles("VIP2", "VIP3")
									.and().withUser("he2").password("admin").roles("VIP3", "VIP4");
	}
	
	
}

3、自定义的登录页面:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>第一个HTML页面</title>
</head>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
自定义表单验证:
<!--<form name="f" action="/login" method="post">-->
    <form name="f" action="/message" method="post">
<br/>
用户名:
<input type="text" name="username" placeholder="name"><br/>
密码:
<input type="password" name="password" placeholder="password"><br/>
<input type="checkbox" name="remember">记住我<br/>
<input name="submit" type="submit" value="提交">
</form>
</body>
</html>

4、首页(关于security的认证函数):

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
	  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">欢迎光临武林秘籍管理系统</h1>
<div sec:authorize="!isAuthenticated()">
	<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/MyLogin}">请登录</a></h2>
</div>
<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>

<hr>

<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>


</body>
</html>
发布了52 篇原创文章 · 获赞 1 · 访问量 1753

猜你喜欢

转载自blog.csdn.net/qq_42039738/article/details/104119372