filterChainDefinitionMap = new LinkedHashMap<>();
30 | filterChainDefinitionMap.put("/logout", "logout");
31 | filterChainDefinitionMap.put("/actuator/**", "anon"); // 允许未认证访问 actuator 端点
32 | filterChainDefinitionMap.put("/**", "authc");
33 | shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
34 |
35 | return shiroFilterFactoryBean;
36 | }
37 |
38 | @Bean
39 | public SecurityManager securityManager() {
40 | DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
41 | securityManager.setRealm(myRealm());
42 | return securityManager;
43 | }
44 |
45 | @Bean
46 | public MyRealm myRealm() {
47 | return new MyRealm();
48 | }
49 |
50 | @Bean
51 | public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
52 | AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
53 | advisor.setSecurityManager(securityManager);
54 | return advisor;
55 | }
56 | }
--------------------------------------------------------------------------------
/src/main/java/com/example/demo/controller/LoginController.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.controller;
2 |
3 | import org.apache.shiro.SecurityUtils;
4 | import org.apache.shiro.authc.UsernamePasswordToken;
5 | import org.apache.shiro.subject.Subject;
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.PostMapping;
9 |
10 | @Controller
11 | public class LoginController {
12 |
13 | @GetMapping("/login")
14 | public String login() {
15 | return "login";
16 | }
17 |
18 | @PostMapping("/login")
19 | public String login(String username, String password) {
20 | Subject subject = SecurityUtils.getSubject();
21 | UsernamePasswordToken token = new UsernamePasswordToken(username, password);
22 | try {
23 | subject.login(token);
24 | return "redirect:/index";
25 | } catch (Exception e) {
26 | return "login";
27 | }
28 | }
29 |
30 | @GetMapping("/index")
31 | public String index() {
32 | return "index";
33 | }
34 |
35 | @GetMapping("/403")
36 | public String unauthorized() {
37 | return "403";
38 | }
39 | }
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # 应用服务 WEB 访问端口
2 | server.port=8080
3 |
4 | spring.thymeleaf.enabled=true
5 | spring.thymeleaf.cache=false
6 | # Actuator 配置
7 | management.endpoints.web.exposure.include=*
--------------------------------------------------------------------------------
/src/main/resources/templates/403.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 403 Forbidden
5 |
6 |
7 | 403 Forbidden
8 | You do not have permission to access this page.
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/main/resources/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Index
5 |
6 |
7 | Welcome to the Index Page
8 | Logout
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/main/resources/templates/login.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Login
5 |
6 |
7 |
14 |
15 |
16 |
--------------------------------------------------------------------------------