一、环境搭建
①前端部分
所有页面代码类似
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 style="text-align: center">这是level1-1</h1>
</body>
</html>
首页代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
这是一个首页
<div>
<h1>leve1</h1>
<a th:href="@{/level1/1}">1</a>
<a th:href="@{/level1/2}">2</a>
<a th:href="@{/level1/3}">3</a>
</div>
<div>
<h1>leve2</h1>
<a th:href="@{/level2/1}">1</a>
<a th:href="@{/level2/2}">2</a>
<a th:href="@{/level2/3}">3</a>
</div>
<div>
<h1>leve3</h1>
<a th:href="@{/level3/1}">1</a>
<a th:href="@{/level3/2}">2</a>
<a th:href="@{/level3/3}">3</a>
</div>
<div>
<a th:href="@{/logout}">注销</a>
<a th:href="@{/login}">登录</a>
</div>
</body>
</html>
路径控制
package com.gg.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RouterController {
// 前往首页
@RequestMapping({"/","/index"})
public String index(){
return "index";
}
// 前往登陆页面
@RequestMapping("/toLogin")
public String toLogin(){
return "views/login";
}
// 前往leve1
@RequestMapping("/level1/{id}")
public String level1(@PathVariable("id") int id){
return "views/level1/"+id;
}
// 前往leve2
@RequestMapping("/level2/{id}")
public String level2(@PathVariable("id") int id){
return "views/level2/"+id;
}
// 前往leve3
@RequestMapping("/level3/{id}")
public String level3(@PathVariable("id") int id){
return "views/level3/"+id;
}
}
②前端的页面访问
二、授权和认证
重点:使用注解
package com.gg.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 // 开启安全支持 也就是说能够使用
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// 链式编程 这个是基于HTTP请求的一同胡访问控制
// 授权
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
// 首页所有人可以访问
.antMatchers("/").permitAll()
// 功能页面只有对应权限的人才能访问
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
// 没有权限会到登陆页面
http.formLogin();
// 开启注销功能
http.logout();
}
// 认证
// 密码编译:PasswordEncoder
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 基于内存认证
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
// 对应的是用户:姓名 密码 角色 通过 and 拼接多个用户
.withUser("lizhenguo").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3","vip1")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
// 使用密码 密码需要设置编码器 也就是加密
}
}
三