1、准备
(1)新建一个SpringBoot项目
(2)导入基础依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- mysql驱动 依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
2、整合shiro
(1)导入依赖
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.3</version> </dependency>
(2)shiro配置
SpringBoot默认没有提供Shiro的自动配置,因为Spring有自己的安全框架
shiro的配置文件:
[users] zhangsan=123
配置过滤器(realm、SecurityManager、过滤器):
@Configuration public class ShiroConfig { @Bean public IniRealm getIniRealm(){//配置realm,提供给SecurityManager IniRealm iniRealm=new IniRealm("classpath:shiro.ini"); return iniRealm; } @Bean public DefaultWebSecurityManager getDefaultWebSecurityManager(IniRealm iniRealm){ DefaultWebSecurityManager defaultSecurityManager=new DefaultWebSecurityManager(); defaultSecurityManager.setRealm(iniRealm);//SecurityManager完成校验需要realm return defaultSecurityManager; } @Bean public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultSecurityManager securityManager){ ShiroFilterFactoryBean filter=new ShiroFilterFactoryBean(); //过滤器是shiro执行权限的核心,进行认证和授权是需要SecurityManager的 filter.setSecurityManager(securityManager); //设置shiro的拦截规则 Map<String,String> filterMap=new HashMap<>(); //user:使用remberme的用户可访问 //perms:对应权限可访问 //role:对应的角色才能访问 filterMap.put("/","anon");//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");//authc表示认证用户可访问 filter.setFilterChainDefinitionMap(filterMap); filter.setLoginUrl("/login.html"); //设置未授权访问的页面 filter.setUnauthorizedUrl("/login.html"); return filter; }
获取到用户的登录信息(这里是从配置文件读取的用户数据),可以获取到realm
根据获取到的realm完成SecurityManager的校验,也就是说用户输入的信息与shiro的配置文件中的数据要通过SecurityManager进行校验
在过滤器中配置有哪些页面或资源不进行拦截(所有用户都可以访问),哪一些页面只有认证的用户可以访问以及访问未授权的页面要跳转的页面
(3)设置页面跳转所需要的controller
@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"; } }
(4)service设置登录校验的方法
public void checkLogin(String username,String password) throws Exception{ Subject subject= SecurityUtils.getSubject(); UsernamePasswordToken token=new UsernamePasswordToken(username,password); subject.login(token); }
(5)用户的controller获取到用户提交的登录信息并调用service层的方法,完成登录的校验
@Controller @RequestMapping("user") public class UserController { @Autowired private UserService userService; @RequestMapping("login") public String login(String username,String password){ try{ userService.checkLogin(username,password); System.out.println("成功"); System.out.println(username+password); return "index"; }catch (Exception e){ e.printStackTrace(); System.out.println("失败"); System.out.println(username+password); return "login"; } } }
登录页面:
<body> <form action="/user/login"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit" value="提交"> </form> </body>
index页面:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <h3>index</h3> </body> </html>
3、测试
(1)输入正确的用户信息
能够正常进入index页面:
可以看到登录的用户名和密码与shiro中的一致
(2)输入错误的用户名和密码
在密码错误的情况下没有跳转ihndex页面,而是停留在了login页面
(3)访问一个不存在的页面
跳转到了登录页: