认证:证明你是谁,携带用户名和密码。系统查验是你这个人的过程。
授权:认证以后你能干什么,访问资源的权限
1,项目
1.1登陆
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
编写配置类
package com.atguigu.security.config;
import org.springframework.context.annotation.Configuration;
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.WebSecurityConfigurerAdapter;
@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
//定制请求规则,访问首页允许所有人可以访问
http.authorizeRequests().antMatchers("/")
.permitAll().antMatchers("/level1/**")
.hasRole("VIP1").antMatchers("/level2/**")
.hasRole("VIP2").antMatchers("/level3/**")
.hasRole("VIP3");
//开启登录功能
http.formLogin();
}
@Override
public void configure(AuthenticationManagerBuilder auth)throws Exception{
// auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("ljs").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP2","VIP3");
// auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("lisi").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP3");
// auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2");
auth.inMemoryAuthentication().withUser("zhangsan")
.password("123456").roles("VIP1","VIP2").
and().withUser("lisi").password("123456")
.roles("VIP1","VIP3").and().withUser("wangwu")
.password("123456").roles("VIP2","VIP3");
}
}
1.2,注销
config里
//开启注销功能(包括跳转规则)
http.logout().logoutSuccessUrl("/");
welcome.html
请登录下写
<form th:action="@{/logout}" method="post">
<input type="submit" value="注销"/>
</form>
启动项目可以使用注销按钮了
1.3,认证授权
1,用户未登陆,显示登陆,用户登陆显示注销
2,权限控制,对应权限展示对应页面内容
pom.xml
<properties>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
<thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version>
</properties>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
页面
welcome.html改命名空间,使用模板引擎
xmlns:sec=“http://www.thymeleaf.org/thymeleaf-extras-springsecurity4”
<h1 align="center">欢迎光临武林秘籍管理系统</h1>
<!--权限登录的修改处-->
<!--没有登录状态的-->
<div sec:authorize="!isAuthenticated()">
<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/userlogin}">请登录</a></h2>
</div>
<!--有登录状态的-->
<div sec:authorize="isAuthenticated()">
<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>
重启项目实现授权功能基本完成
1.4,记住我功能
登陆一次,下次重启浏览器还是登陆状态
config
//开启记住我功能
http.rememberMe();
重启项目,点登陆的时候发现多了一个单选框。选择它就好
cookie默认保存14天
//运行流程:
1,登陆成功以后,将cookie发给浏览器保存,以后访问这个页面带上这个cookie,通过检查就免登陆
2,点击注销会删除cookie
之前登陆都进的是系统登陆页面。怎么进定制登陆页面
可以自己写一个页面
修改处
1,
//开启登录功能
http.formLogin().loginPage("/userlogin");
2,
welcome.html
<a th:href="@{/userlogin}">
请登录
3,修改login.html
<form th:action="@{/userlogin}" method="post">
自定义尽量改详细彻底点
config
//开启登录功能
http.formLogin().loginPage("/userlogin")
.usernameParameter("user").passwordParameter("pwd")
.loginPage("/userlogin");//如果没有权限就回到登录页面
login.html
用户名:<input name="user"/><br/>
密码:<input name="pwd"><br/>
自定义登陆页加记住我功能
login.html
加一个
<input type="checkbox" name="remember"/>记住我<br/>
修改config
http.rememberMe().rememberMeParameter("remember");