mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 spring security 的代码
This commit is contained in:
30
lab-33/lab-33-shiro-demo/pom.xml
Normal file
30
lab-33/lab-33-shiro-demo/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.10.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-33-shiro-demo</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 Spring MVC 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Shiro 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.shiro</groupId>
|
||||
<artifactId>shiro-spring-boot-starter</artifactId>
|
||||
<version>1.4.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springboot.lab01.shirodemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package cn.iocoder.springboot.lab01.shirodemo.config;
|
||||
|
||||
import org.apache.shiro.realm.Realm;
|
||||
import org.apache.shiro.realm.SimpleAccountRealm;
|
||||
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
||||
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class ShiroConfig {
|
||||
|
||||
@Bean
|
||||
public Realm realm() {
|
||||
SimpleAccountRealm realm = new SimpleAccountRealm();
|
||||
realm.addAccount("admin", "admin", "ADMIN");
|
||||
return realm;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DefaultWebSecurityManager securityManager() {
|
||||
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
|
||||
securityManager.setRealm(this.realm());
|
||||
return securityManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ShiroFilterFactoryBean shiroFilterFactoryBean() {
|
||||
// 创建 ShiroFilterFactoryBean 对象,用于创建 ShiroFilter 过滤器
|
||||
ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
|
||||
|
||||
// 设置 SecurityManager
|
||||
filterFactoryBean.setSecurityManager(this.securityManager());
|
||||
|
||||
// 设置 URL 们
|
||||
filterFactoryBean.setLoginUrl("/login"); // 登陆 URL
|
||||
filterFactoryBean.setSuccessUrl("/login_success"); // 登陆成功 URL
|
||||
filterFactoryBean.setUnauthorizedUrl("/unauthorized"); // 无权限 URL
|
||||
|
||||
// 设置 URL 的权限配置
|
||||
filterFactoryBean.setFilterChainDefinitionMap(this.filterChainDefinitionMap());
|
||||
|
||||
return filterFactoryBean;
|
||||
}
|
||||
|
||||
private Map<String, String> filterChainDefinitionMap() {
|
||||
Map<String, String> filterMap = new LinkedHashMap<>(); // 注意要使用有序的 LinkedHashMap ,顺序匹配
|
||||
filterMap.put("/test/echo", "anon"); // 允许匿名访问
|
||||
filterMap.put("/test/admin", "roles[ADMIN]"); // 需要 ADMIN 角色
|
||||
filterMap.put("/test/normal", "roles[NORMAL]"); // 需要 NORMAL 角色
|
||||
filterMap.put("/logout", "logout"); // 退出
|
||||
filterMap.put("/**", "authc"); // 默认剩余的 URL ,需要经过认证
|
||||
return filterMap;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package cn.iocoder.springboot.lab01.shirodemo.controller;
|
||||
|
||||
import org.apache.shiro.authz.annotation.RequiresGuest;
|
||||
import org.apache.shiro.authz.annotation.RequiresRoles;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
|
||||
@RequiresGuest
|
||||
@GetMapping("/echo")
|
||||
public String demo() {
|
||||
return "示例返回";
|
||||
}
|
||||
|
||||
@GetMapping("/home")
|
||||
public String home() {
|
||||
return "我是首页";
|
||||
}
|
||||
|
||||
@RequiresRoles("ADMIN")
|
||||
@GetMapping("/admin")
|
||||
public String admin() {
|
||||
return "我是管理员";
|
||||
}
|
||||
|
||||
@RequiresRoles("NORMAL")
|
||||
@GetMapping("/normal")
|
||||
public String normal() {
|
||||
return "我是普通用户";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package cn.iocoder.springboot.lab01.shirodemo.controller;
|
||||
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authc.ExpiredCredentialsException;
|
||||
import org.apache.shiro.authc.IncorrectCredentialsException;
|
||||
import org.apache.shiro.authc.LockedAccountException;
|
||||
import org.apache.shiro.authc.UnknownAccountException;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/")
|
||||
public class SecurityController {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@GetMapping("/login")
|
||||
public String loginPage() {
|
||||
return "login.html";
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@PostMapping("/login")
|
||||
public String login(HttpServletRequest request) {
|
||||
// 判断是否已经登陆
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
if (subject.getPrincipal() != null) {
|
||||
return "你已经登陆账号:" + subject.getPrincipal();
|
||||
}
|
||||
|
||||
// 获得登陆失败的原因
|
||||
String shiroLoginFailure = (String) request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
|
||||
// 翻译成人类看的懂的提示
|
||||
String msg = "";
|
||||
if (UnknownAccountException.class.getName().equals(shiroLoginFailure)) {
|
||||
msg = "账号不存在";
|
||||
} else if (IncorrectCredentialsException.class.getName().equals(shiroLoginFailure)) {
|
||||
msg = "密码不正确";
|
||||
} else if (LockedAccountException.class.getName().equals(shiroLoginFailure)) {
|
||||
msg = "账号被锁定";
|
||||
} else if (ExpiredCredentialsException.class.getName().equals(shiroLoginFailure)) {
|
||||
msg = "账号已过期";
|
||||
} else {
|
||||
msg = "未知";
|
||||
logger.error("[login][未知登陆错误:{}]", shiroLoginFailure);
|
||||
}
|
||||
return "登陆失败,原因:" + msg;
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@GetMapping("/login_success")
|
||||
public String loginSuccess() {
|
||||
return "登陆成功";
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@GetMapping("/unauthorized")
|
||||
public String unauthorized() {
|
||||
return "你没有权限";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.iocoder.springboot.lab01.shirodemo.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/test")
|
||||
public class TestController {
|
||||
|
||||
@GetMapping("/demo")
|
||||
public String demo() {
|
||||
return "示例返回";
|
||||
}
|
||||
|
||||
@GetMapping("/home")
|
||||
public String home() {
|
||||
return "我是首页";
|
||||
}
|
||||
|
||||
@GetMapping("/admin")
|
||||
public String admin() {
|
||||
return "我是管理员";
|
||||
}
|
||||
|
||||
@GetMapping("/normal")
|
||||
public String normal() {
|
||||
return "我是普通用户";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>登陆页面</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="/login" method="post">
|
||||
用户名:<input type="text" name="username"/> <br />
|
||||
密码:<input type="password" name="password"/> <br />
|
||||
<input type="submit" value="登录"/>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
19
lab-33/pom.xml
Normal file
19
lab-33/pom.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>labs-parent</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-33</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-33-shiro-demo</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user