支持的特性:
-
-
com.alibaba -
druid-spring-boot-starter -
1.1.13 -
-
-
mysql -
mysql-connector-java -
8.0.29 -
-
-
org.mybatis.spring.boot -
mybatis-spring-boot-starter -
2.2.2 -
-
-
org.springframework.boot -
spring-boot-starter-thymeleaf -
-
-
org.springframework.boot -
spring-boot-starter-web -
-
-
-
org.projectlombok -
lombok -
true -
- spring:
- datasource:
- druid:
- url: jdbc:mysql://localhost:3306/db_shiro
- driver-class-name: com.mysql.cj.jdbc.Driver
- username: root
- password: root
- initial-size: 1
- min-idle: 1
- max-active: 20
-
- mybatis:
- mapper-locations: classpath:mappers/*Mapper.xml
- type-aliases-package: com.qfedu.shiro2.beans
-
-
org.apache.shiro -
shiro-spring -
1.4.0 -
- package com.qfedu.shiro2.config;
-
- import org.apache.shiro.realm.text.IniRealm;
- 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.HashMap;
- import java.util.Map;
-
- @Configuration
- public class ShiroConfig {
- @Bean
- public IniRealm getIniReam(){
- IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
- return iniRealm;
- }
- @Bean
- public DefaultWebSecurityManager getDefaultWebSecurityManager(IniRealm iniRealm){
- DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
- securityManager.setRealm(iniRealm);
- return securityManager;
- }
- @Bean
- public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager){
- ShiroFilterFactoryBean filter = new ShiroFilterFactoryBean();
- //过滤器就是shiro进行权限校验的核心,进行认证和授权是需要SecurityManager的
- filter.setSecurityManager(securityManager);
-
- /*设置shiro的拦截规则
- anon 匿名用户可访问
- authc 认证用户可访问
- user 使用RemeberMe的用户可访问
- perms 对应权限可访问
- role 对应角色可访问
- */
- Map
filterMap=new HashMap<>(); - filterMap.put("/", "anon");
- filterMap.put("/login.html", "anon");
- filterMap.put("/regist.html", "anon");
- filterMap.put("/user/login", "anon");
- filterMap.put("user/regist", "anon");
- filterMap.put("/static/**", "anon");
- filterMap.put("/**", "authc");
-
- filter.setFilterChainDefinitionMap(filterMap);
- filter.setLoginUrl("/login.html");
- //设置未授权访问的页面路径
- filter.setUnauthorizedUrl("/login.html");
- return filter;
- }
- }
shiro.ini
- [users]
- zhangsan=123456,seller
- lisi=666666,ckmgr
- admin=222222,admin
-
- [roles]
- admin=*
- seller=order-add,order-del,order-list
- ckmgr=ck-add,ck-del,ck-list
UserController
- package com.qfedu.shiro2.controller;
-
- import com.qfedu.shiro2.service.UserServiceImpl;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.annotation.Resource;
-
- @Controller
- @RequestMapping("/user")
- public class UserController {
- @Resource
- private UserServiceImpl userService;
-
- @RequestMapping("/login")
- public String login(String userName,String userPwd){
- try {
- userService.checkLogin(userName, userPwd);
- System.out.println("登录成功");
- return "index";
- } catch (Exception e) {
- System.out.println("登录失败");
- return "login";
- }
-
- }
- }
PageController
- package com.qfedu.shiro2.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- public class PageController {
- @RequestMapping("/login.html")
- public String login(){
- return "login";
- }
- @RequestMapping("/")
- public String login1(){
- return "login";
- }
- @RequestMapping("/index.html")
- public String index(){
- return "index";
- }
- }
UserServiceImpl
- package com.qfedu.shiro2.service;
-
- import org.apache.shiro.SecurityUtils;
- import org.apache.shiro.authc.UsernamePasswordToken;
- import org.apache.shiro.subject.Subject;
- import org.springframework.stereotype.Service;
-
- @Service
- public class UserServiceImpl {
-
- public void checkLogin(String userName,String userPwd) throws Exception{
- Subject subject = SecurityUtils.getSubject();
- UsernamePasswordToken token = new UsernamePasswordToken(userName, userPwd);
- subject.login(token);
- }
- }
login.html
- "en">
- "UTF-8">
-
Title - login
-
-
-
账号:"text" name="userName"/>
-
密码:"text" name="userPwd"/>
-
"submit" value="登录"/>
-