shiro

  1. Subject currentUser = SecurityUtils.getSubject();
  2. Session session = currentUser.getSession();
  3. currentUser.isAuthenticated()
  4. currentUser.getPrincipal()
  5. currentUser.hasRole("schwartz")
  6. currentUser.isPermitted("lightsaber:wield")
  7. currentUser.isPermitted("winnebago:drive:eagle5")
  8. currentUser.logout();

shiro整合springboot

1)导入依赖

  1. <!--
  2. Subjects
  3. shiro securityManager
  4. Realm
  5. -->
  6. <!--shiro整合spring的包-->
  7. <dependency>
  8. <groupId>org.apache.shiro</groupId>
  9. <artifactId>shiro-spring</artifactId>
  10. <version>1.8.0</version>
  11. </dependency>

2)自定义Realm

  1. //自定义realm
  2. public class UserRealm extends AuthorizingRealm{
  3. //授权
  4. @Override
  5. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
  6. System.out.println("执行了=>>授权doGetAuthorizationInfo");
  7. return null;
  8. }
  9. //认证
  10. @Override
  11. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
  12. System.out.println("执行了=>>认证doGetAuthenticationInfo");
  13. return null;
  14. }
  15. }

3)配置类

  1. @Configuration
  2. public class MyConfig {
  3. //ShiroFilterFactoryBean:第三步
  4. @Bean
  5. public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
  6. ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
  7. //设置安全管理器
  8. bean.setSecurityManager(defaultWebSecurityManager);
  9. return bean;
  10. }
  11. //defaultWebSecurityBean:第二步
  12. @Bean(name = "securityManager")
  13. public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
  14. DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
  15. //关联UserRealm
  16. securityManager.setRealm(userRealm);
  17. return securityManager;
  18. }
  19. //创建 realm对象,需要自定义:第一步
  20. @Bean
  21. public UserRealm userRealm(){
  22. return new UserRealm();
  23. }
  24. }

4)controller与html

登录拦截

1)配置过滤器

  1. @Bean
  2. public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
  3. ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
  4. //设置安全管理器
  5. bean.setSecurityManager(defaultWebSecurityManager);
  6. //添加shiro的内置过滤器
  7. /*
  8. anno:无需认证就可访问
  9. authc:必须认证了才能访问
  10. user:必须拥有记住我功能才能用
  11. perms:拥有对某个资源的权限才能访问
  12. role:拥有某个角色权限才能访问
  13. */
  14. Map<String, String> filterMap = new LinkedHashMap<>();
  15. filterMap.put("/user/add","authc");
  16. filterMap.put("/user/update","authc");
  17. bean.setFilterChainDefinitionMap(filterMap);
  18. bean.setLoginUrl("/toLogin");
  19. return bean;
  20. }

2)login.html及controller

shiro整合mybatis

1)导入mybatis,mysql,lombok,log4j依赖

2)进行配置

image.png
image.png

3)mapper层,service层,controller层

image.png

4)认证

  1. @Autowired
  2. UserService userService;
  3. //认证
  4. @Override
  5. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
  6. System.out.println("执行了=>>认证doGetAuthenticationInfo");
  7. UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;
  8. User user = userService.queryUserByName(userToken.getUsername());
  9. if(user == null){
  10. return null;
  11. }
  12. //可以加密:MD5盐值加密
  13. //密码认证 shiro做
  14. return new SimpleAuthenticationInfo("",user.getPassword(),"");
  15. }

5)授权

未授权页面
image.png
shiroConfig中设置页面的权限
image.png
自定义Realm中设置用户权限
image.png

shiro整合thymeleaf

1)导包

  1. <dependency>
  2. <groupId>com.github.theborakompanioni</groupId>
  3. <artifactId>thymeleaf-extras-shiro</artifactId>
  4. <version>2.1.0</version>
  5. </dependency>

2)首页中使用thymeleaf与shiro的整合

命名空间(很重要)

  1. <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <h1>首页</h1>
  9. <!--如果没认证,就显示登录按钮-->
  10. <div shiro:notAuthenticated="">
  11. <a th:href="@{/toLogin}">登录</a>
  12. </div>
  13. <p th:text="${msg}"></p>
  14. <!--如果有add页面权限,就显示add页面的按钮-->
  15. <div shiro:hasPermission="user:add">
  16. <a th:href="@{/user/add}">add</a>
  17. </div>
  18. <!--如果有update页面权限,就显示update页面的按钮-->
  19. <div shiro:hasPermission="user:update">
  20. <a th:href="@{/user/update}">update</a>
  21. </div>
  22. </body>
  23. </html>