SpringCloud Alibaba(八) - Durid,SpringSecurity,Shiro

打印 上一主题 下一主题

主题 533|帖子 533|积分 1599

1、Durid

1.1 简介

Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池。
Druid 是阿里巴巴开源平台上一个数据库连接池实现,结合了 C3P0、DBCP 等 DB 池的优点,同时加入了日志监控。
Druid 可以很好的监控 DB 池连接和 SQL 的执行情况,天生就是针对监控而生的 DB 连接池。
Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验。
Spring Boot 2.0 以上默认使用 Hikari 数据源,可以说 Hikari 与 Driud 都是当前 Java Web 上最优秀的数据源,我们来重点介绍 Spring Boot 如何集成 Druid 数据源,如何实现数据库监控。
Github地址:https://github.com/alibaba/druid/

1.2 依赖
  1. <dependency>
  2.     <groupId>com.alibaba</groupId>
  3.     <artifactId>druid</artifactId>
  4.     <version>1.2.8</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>log4j</groupId>
  8.     <artifactId>log4j</artifactId>
  9.     <version>1.2.17</version>
  10. </dependency>
复制代码
1.3 配置

1.3.1 application.yml
  1. # 端口
  2. server:
  3.   port: 9603
  4. # 服务名
  5. spring:
  6.   application:
  7.     name: kgcmall96-user
  8.   # 数据源配置
  9.   datasource:
  10.     driver-class-name: com.mysql.cj.jdbc.Driver
  11.     url: jdbc:mysql://localhost:3306/kh96_alibaba_kgcmalldb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
  12.     username: root
  13.     password: root
  14.     #2、切换数据源;之前已经说过 Spring Boot 2.0 以上默认使用 com.zaxxer.hikari.HikariDataSource 数据源,但可以 通过 spring.datasource.type 指定数据源。
  15.     # 指定数据源类型
  16.     type: com.alibaba.druid.pool.DruidDataSource
  17.     # 数据源其他配置
  18.     # 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
  19.     initialSize: 5
  20.     # 最小连接池数量
  21.     minIdle: 5
  22.     # 最大连接池数量
  23.     maxActive: 20
  24.     # 获取连接时最大等待时间,单位毫秒。
  25.     maxWait: 60000
  26.     # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  27.     timeBetweenEvictionRunsMillis: 60000
  28.     # 配置一个连接在池中最小生存的时间,单位是毫秒
  29.     minEvictableIdleTimeMillis: 300000
  30.     # 用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。
  31.     validationQuery: SELECT 1 FROM DUAL
  32.     # 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
  33.     testWhileIdle: true
  34.     # 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
  35.     testOnBorrow: false
  36.     # 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
  37.     testOnReturn: false
  38.     # 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭
  39.     poolPreparedStatements: false
  40.     maxPoolPreparedStatementPerConnectionSize: 20
  41.     # 合并多个DruidDataSource的监控数据
  42.     useGlobalDataSourceStat: true
  43.     # 配置监控统计拦截的filters,去掉后监控界面sql无法统计
  44.     # 属性性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall
  45.     filters: stat,wall,log4j
  46.     # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
  47.     connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
  48.   # jpa配置
  49.   jpa:
  50.     hibernate:
  51.       ddl-auto: update
  52.     show-sql: true
复制代码
1.3.2 log4j.properties

在网上随便搜的一个;
  1. log4j.rootLogger=DEBUG, stdout
  2. # Console output...
  3. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
复制代码
1.4 Druid数据源配置类
  1. /**
  2. * @author : huayu
  3. * @date   : 29/11/2022
  4. * @param  :
  5. * @return :
  6. * @description :  Druid数据源配置类
  7. */
  8. @Configuration
  9. public class DruidConfig {
  10.     @Bean
  11.     @ConfigurationProperties(prefix = "spring.datasource")
  12.     public DataSource dataSource() {
  13.         return new DruidDataSource();
  14.     }
  15.     /**
  16.      * 配置后台管理
  17.      */
  18.     @Bean
  19.     public ServletRegistrationBean statViewServlet() {
  20.         ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
  21.         // 初始化参数
  22.         Map<String, String> initParams = new HashMap<>();
  23.         initParams.put("loginUsername", "admin");
  24.         initParams.put("loginPassword", "123456");
  25.         // 是否允许访问
  26.         initParams.put("allow", "");
  27.         bean.setInitParameters(initParams);
  28.         return bean;
  29.     }
  30.     /**
  31.      * 配置web监控的过滤器
  32.      */
  33.     @Bean
  34.     public FilterRegistrationBean webStatFilter() {
  35.         FilterRegistrationBean bean = new FilterRegistrationBean();
  36.         bean.setFilter(new WebStatFilter());
  37.         // 初始化参数
  38.         Map<String, String> initParams = new HashMap<>();
  39.         // 排除过滤,静态文件等
  40.         initParams.put("exclusions", "*.css,*.js,/druid/*");
  41.         bean.setInitParameters(initParams);
  42.         bean.setUrlPatterns(Arrays.asList("/*"));
  43.         return bean;
  44.     }
  45. }
复制代码
1.5 测试

测试代码不在赘述,就简单写一个测试请求就好;
1.5.1 登录


1.5.2 测试请求


2、SpringSecurity

2.0 认识SpringSecurity

Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入 spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理!
记住几个类:

  • WebSecurityConfigurerAdapter:自定义Security策略
  • AuthenticationManagerBuilder:自定义认证策略
  • @EnableWebSecurity:开启WebSecurity模式
Spring Security的两个主要目标是 “认证” 和 “授权”(访问控制)。
“认证”(Authentication)
身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。
身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。
“授权” (Authorization)
授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。
这个概念是通用的,而不是只在Spring Security 中存在。
2.1 项目介绍


2.1 依赖
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>
  5. <dependency>
  6.     <groupId>org.springframework.boot</groupId>
  7.     <artifactId>spring-boot-starter-security</artifactId>
  8. </dependency>
  9. <dependency>
  10.     <groupId>org.thymeleaf.extras</groupId>
  11.     <artifactId>thymeleaf-extras-springsecurity5</artifactId>
  12.     <version>3.0.4.RELEASE</version>
  13. </dependency>
复制代码
2.2 application.yml
  1. # 关闭模板缓存
  2. spring:
  3.   thymeleaf:
  4.     cache: false
复制代码
2.3 html

2.3.1 1.html

创建这几个文件;

1.html(这几个html的代码都是一样的)
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  6.     <title>首页</title>
  7.    
  8.     <link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
  9.     <link th:href="@{/qinjiang/css/qinstyle.css}" rel="stylesheet">
  10. </head>
  11. <body>
  12.    
  13.    
  14.         <h3>Level-1-1</h3>
  15.    
  16. </body>
  17. </html>
复制代码
2.3.2  login.html
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  6.     <title>登录</title>
  7.    
  8.     <link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
  9. </head>
  10. <body>
  11.    
  12.         
  13.             <h1 >登录</h1>
  14.         
  15.         
  16.             
  17.                
  18.                     
  19.                         <form th:action="@{/login}" method="post">
  20.                            
  21.                                 <label>Username</label>
  22.                                 
  23.                                     <input type="text" placeholder="Username" name="username">
  24.                                     <i ></i>
  25.                                 
  26.                            
  27.                            
  28.                                 <label>Password</label>
  29.                                 
  30.                                     <input type="password" name="password">
  31.                                     <i ></i>
  32.                                 
  33.                            
  34.                            
  35.                                 <input type="checkbox" name="remember"> 记住我
  36.                            
  37.                             <input type="submit" />
  38.                         </form>
  39.                     
  40.                
  41.             
  42.         
  43.         
  44.             
  45.                 </i>注册
  46.             
  47.             <br><br>
  48.             <small>blog.kuangstudy.com</small>
  49.         
  50.         
  51.             <h3>Spring Security Study by 秦疆</h3>
  52.         
  53.    
  54. </body>
  55. </html>
复制代码
2.3.3 index.html
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org"
  3.       xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  7.     <title>首页</title>
  8.    
  9.     <link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
  10.     <link th:href="@{/qinjiang/css/qinstyle.css}" rel="stylesheet">
  11. </head>
  12. <body>
  13.    
  14.         
  15.             <a   th:target="_blank" href="https://www.cnblogs.com/@{/index}">首页</a>
  16.             
  17.             
  18.                
  19.                
  20.                     <a  th:target="_blank" href="https://www.cnblogs.com/@{/toLogin}">
  21.                         <i ></i> 登录
  22.                     </a>
  23.                
  24.                
  25.                
  26.                     <a >
  27.                         用户名:
  28.                         角色:
  29.                     </a>
  30.                     <a  th:target="_blank" href="https://www.cnblogs.com/@{/logout}">
  31.                         <i ></i> 注销
  32.                     </a>
  33.                
  34.                
  35.             
  36.         
  37.    
  38.    
  39.         <h3>Spring Security Study by 秦疆</h3>
  40.    
  41.    
  42.         <br>
  43.         
  44.             
  45.                
  46.                     
  47.                         
  48.                             <h5 >Level 1</h5>
  49.                             <hr>
  50.                             <a th:target="_blank" href="https://www.cnblogs.com/@{/level1/1}"><i ></i> Level-1-1</a>
  51.                             <a th:target="_blank" href="https://www.cnblogs.com/@{/level1/2}"><i ></i> Level-1-2</a>
  52.                             <a th:target="_blank" href="https://www.cnblogs.com/@{/level1/3}"><i ></i> Level-1-3</a>
  53.                         
  54.                     
  55.                
  56.             
  57.             
  58.                
  59.                     
  60.                         
  61.                             <h5 >Level 2</h5>
  62.                             <hr>
  63.                             <a th:target="_blank" href="https://www.cnblogs.com/@{/level2/1}"><i ></i> Level-2-1</a>
  64.                             <a th:target="_blank" href="https://www.cnblogs.com/@{/level2/2}"><i ></i> Level-2-2</a>
  65.                             <a th:target="_blank" href="https://www.cnblogs.com/@{/level2/3}"><i ></i> Level-2-3</a>
  66.                         
  67.                     
  68.                
  69.             
  70.             
  71.                
  72.                     
  73.                         
  74.                             <h5 >Level 3</h5>
  75.                             <hr>
  76.                             <a th:target="_blank" href="https://www.cnblogs.com/@{/level3/1}"><i ></i> Level-3-1</a>
  77.                             <a th:target="_blank" href="https://www.cnblogs.com/@{/level3/2}"><i ></i> Level-3-2</a>
  78.                             <a th:target="_blank" href="https://www.cnblogs.com/@{/level3/3}"><i ></i> Level-3-3</a>
  79.                         
  80.                     
  81.                
  82.             
  83.         
  84.    
  85.    
  86. </body>
  87. </html>
复制代码
2.4 RouterController
  1. @Controller
  2. public class RouterController {
  3.     //首页
  4.     @RequestMapping({"/","/index"})
  5.     public String index(){
  6.         return "index";
  7.     }
  8.     //登录
  9.     @RequestMapping("/toLogin")
  10.     public String toLogin(){
  11.         return "views/login";
  12.     }
  13.     //level1 权限才可以访问
  14.     @RequestMapping("/level1/{id}")
  15.     public String leave1(@PathVariable("id") int id){
  16.         return "views/level1/"+id;
  17.     }
  18.     //level2 权限才可以访问
  19.     @RequestMapping("/level2/{id}")
  20.     public String leave2(@PathVariable("id") int id){
  21.         return "views/level2/"+id;
  22.     }
  23.     //level3 权限才可以访问
  24.     @RequestMapping("/level3/{id}")
  25.     public String leave3(@PathVariable("id") int id){
  26.         return "views/level3/"+id;
  27.     }
  28. }
复制代码
2.5 SecurityConfig
  1. //AOP : 拦截器
  2. @EnableWebSecurity
  3. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  4.     //授权
  5.     @Override
  6.     protected void configure(HttpSecurity http) throws Exception {
  7.         //首页所有人可以访问,功能页只有对应权限的人才能访问
  8.         //请求授权的规则
  9.         http.authorizeRequests()
  10.                 .antMatchers("/").permitAll()  //所有的人可以访问
  11.                 .antMatchers("/level1/**").hasRole("vip1")   //有相对的权限才可以访问
  12.                 .antMatchers("/level2/**").hasRole("vip2")
  13.                 .antMatchers("/level3/**").hasRole("vip3");
  14.         //没有权限默认会到登录页面
  15.         //  /login
  16.         http.formLogin()
  17.                 .loginPage("/toLogin") //走的登录页面
  18.                 .usernameParameter("username").passwordParameter("password") //默认的帮我们写的登录验真参数为username,password,通过这个可以改变参数名字,例如user,pwd
  19.                 .loginProcessingUrl("/login");  //真正的登录页面
  20.         //防止网站工具: get post
  21.         http.csrf().disable(); //关闭csrf功能 防止攻击  登录失败可能存在的原因
  22.         //注销,开启了注销功能,跳到首页
  23.         http.logout()
  24.                 .deleteCookies("remove")  //移除cookies
  25.                 .invalidateHttpSession(true)  //清除session
  26.                 .logoutSuccessUrl("/");
  27.         //开启记住我功能
  28.         http.rememberMe()  //默认保存两周  //这些直接开启的都是自动配置里面的那个登录页面,只需要开启就可以;
  29.         .rememberMeParameter("remember"); //自定义接收前端的参数  //这些需要自定义参数的都是我们自己写的登录页面
  30.     }
  31.     //认证 , springboot 2.1.x可以使用
  32.     //密码编码: PasswowrdEncoder   报错: 密码前面直接加{noop},就可以了 password("{noop}123456")
  33.     //在Spring Secutiry 5.0+ 新增了很多的加密方法
  34.     //定义认证规则
  35.     @Override
  36.     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  37.         //这些数据正常应该从数据库中读
  38.         auth
  39. //                .jdbcAuthentication() //从数据库中读数据
  40.                 .inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
  41.                 .withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
  42.                 .and()
  43.                 .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
  44.                 .and()
  45.                 .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
  46.     }
  47.    
  48. }
复制代码
2.6 测试

2.6.1 不同权限的用户登录

2.6.1.1  只有 vip1 权限

登录:guest 用户

登录成功:

2.6.1.2  只有 vip1, vip2 权限

登录:kuangshen 用户

登陆成功:

2.6.1.3  只有 vip1, vip2, vip3 权限

登录:root 用户

登陆成功

2.6.2 记住我

2.6.2.1 登录


2.6.2.2 登录成功


2.6.2.3 关闭浏览器,重新访问 http://localhost:8080/

登录成功

检查Cookie

参考博客地址:https://mp.weixin.qq.com/s/FLdC-24_pP8l5D-i7kEwcg
视频地址:https://www.bilibili.com/video/BV1PE411i7CV/?p=34
3、Shiro

3.1 helloShiro快速开始


3.1.1 依赖
  1. <dependency>
  2.     <groupId>org.apache.shiro</groupId>
  3.     <artifactId>shiro-core</artifactId>
  4.     <version>1.7.1</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.slf4j</groupId>
  8.     <artifactId>jcl-over-slf4j</artifactId>
  9.     <version>1.7.21</version>
  10. </dependency>
  11. <dependency>
  12.     <groupId>org.slf4j</groupId>
  13.     <artifactId>slf4j-log4j12</artifactId>
  14.     <version>1.7.21</version>
  15. </dependency>
  16. <dependency>
  17.     <groupId>log4j</groupId>
  18.     <artifactId>log4j</artifactId>
  19.     <version>1.2.17</version>
  20. </dependency>
复制代码
3.1.2 log4j.properties
  1. log4j.rootLogger=INFO, stdout
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n
  5. # General Apache libraries
  6. log4j.logger.org.apache=WARN
  7. # Spring
  8. log4j.logger.org.springframework=WARN
  9. # Default Shiro logging
  10. log4j.logger.org.apache.shiro=INFO
  11. # Disable verbose logging
  12. log4j.logger.org.apache.shiro.util.ThreadContext=WARN
  13. log4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN
复制代码
3.1.3 shiro.ini
  1. [users]
  2. # user 'root' with password 'secret' and the 'admin' role
  3. root = secret, admin
  4. # user 'guest' with the password 'guest' and the 'guest' role
  5. guest = guest, guest
  6. # user 'presidentskroob' with password '12345' ("That's the same combination on
  7. # my luggage!!!" ;)), and role 'president'
  8. presidentskroob = 12345, president
  9. # user 'darkhelmet' with password 'ludicrousspeed' and roles 'darklord' and 'schwartz'
  10. darkhelmet = ludicrousspeed, darklord, schwartz
  11. # user 'lonestarr' with password 'vespa' and roles 'goodguy' and 'schwartz'
  12. lonestarr = vespa, goodguy, schwartz
  13. # -----------------------------------------------------------------------------
  14. # Roles with assigned permissions
  15. #
  16. # Each line conforms to the format defined in the
  17. # org.apache.shiro.realm.text.TextConfigurationRealm#setRoleDefinitions JavaDoc
  18. # -----------------------------------------------------------------------------
  19. [roles]
  20. # 'admin' role has all permissions, indicated by the wildcard '*'
  21. admin = *
  22. # The 'schwartz' role can do anything (*) with any lightsaber:
  23. schwartz = lightsaber:*
  24. # The 'goodguy' role is allowed to 'drive' (action) the winnebago (type) with
  25. # license plate 'eagle5' (instance specific id)
  26. goodguy = winnebago:drive:eagle5
复制代码
3.1.4 Quickstart
  1. /**
  2. * Simple Quickstart application showing how to use Shiro's API.
  3. *
  4. * @since 0.9 RC2
  5. */
  6. public class Quickstart {
  7.     private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);
  8.     public static void main(String[] args) {
  9.         Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
  10.         SecurityManager securityManager = factory.getInstance();
  11.         SecurityUtils.setSecurityManager(securityManager);
  12.         // Now that a simple Shiro environment is set up, let's see what you can do:
  13.         // get the currently executing user:
  14.         //获取当前的用户对象 Subject
  15.         Subject currentUser = SecurityUtils.getSubject();
  16.         // Do some stuff with a Session (no need for a web or EJB container!!!)
  17.         //通过当前用户拿到session
  18.         Session session = currentUser.getSession();
  19.         session.setAttribute("someKey", "aValue");
  20.         String value = (String) session.getAttribute("someKey");
  21.         if (value.equals("aValue")) {
  22.             log.info("Retrieved the correct value! [" + value + "]");
  23.         }
  24.         //判断当前用户是否被认证
  25.         //Token:没有获取,直接设置令牌
  26.         if (!currentUser.isAuthenticated()) {
  27.             UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
  28.             token.setRememberMe(true);//设置记住我
  29.             try {
  30.                 currentUser.login(token);//执行登录操作
  31.             } catch (UnknownAccountException uae) {
  32.                 log.info("There is no user with username of " + token.getPrincipal());
  33.             } catch (IncorrectCredentialsException ice) {
  34.                 log.info("Password for account " + token.getPrincipal() + " was incorrect!");
  35.             } catch (LockedAccountException lae) {
  36.                 log.info("The account for username " + token.getPrincipal() + " is locked.  " +
  37.                         "Please contact your administrator to unlock it.");
  38.             }
  39.             // ... catch more exceptions here (maybe custom ones specific to your application?
  40.             catch (AuthenticationException ae) {
  41.                 //unexpected condition?  error?
  42.             }
  43.         }
  44.         //say who they are:
  45.         //print their identifying principal (in this case, a username):
  46.         log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
  47.         //test a role:
  48.         if (currentUser.hasRole("schwartz")) {
  49.             log.info("May the Schwartz be with you!");
  50.         } else {
  51.             log.info("Hello, mere mortal.");
  52.         }
  53.         //粗粒度
  54.         //test a typed permission (not instance-level)
  55.         if (currentUser.isPermitted("lightsaber:wield")) {
  56.             log.info("You may use a lightsaber ring.  Use it wisely.");
  57.         } else {
  58.             log.info("Sorry, lightsaber rings are for schwartz masters only.");
  59.         }
  60.         //细粒度
  61.         //a (very powerful) Instance Level permission:
  62.         if (currentUser.isPermitted("winnebago:drive:eagle5")) {
  63.             log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
  64.                     "Here are the keys - have fun!");
  65.         } else {
  66.             log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
  67.         }
  68.         //注销
  69.         //all done - log out!
  70.         currentUser.logout();
  71.         //结束
  72.         System.exit(0);
  73.     }
  74. }
复制代码

3.1.5 测试


3.2 整合SpringBoot,MyBatis,Thymeleaf


3.2.1 依赖
  1. <dependency>
  2.     <groupId>org.apache.shiro</groupId>
  3.     <artifactId>shiro-spring</artifactId>
  4.     <version>1.7.1</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.thymeleaf</groupId>
  8.     <artifactId>thymeleaf-spring5</artifactId>
  9. </dependency>
  10. <dependency>
  11.     <groupId>org.thymeleaf.extras</groupId>
  12.     <artifactId>thymeleaf-extras-java8time</artifactId>
  13. </dependency>
  14. <dependency>
  15.     <groupId>org.slf4j</groupId>
  16.     <artifactId>slf4j-log4j12</artifactId>
  17.     <version>1.7.21</version>
  18. </dependency>
  19. <dependency>
  20.     <groupId>log4j</groupId>
  21.     <artifactId>log4j</artifactId>
  22.     <version>1.2.17</version>
  23. </dependency>
  24. <dependency>
  25.     <groupId>com.alibaba</groupId>
  26.     <artifactId>druid</artifactId>
  27.     <version>1.1.21</version>
  28. </dependency>
  29. <dependency>
  30.     <groupId>mysql</groupId>
  31.     <artifactId>mysql-connector-java</artifactId>
  32.     <scope>runtime</scope>
  33. </dependency>
  34. <dependency>
  35.     <groupId>org.springframework.boot</groupId>
  36.     <artifactId>spring-boot-starter-jdbc</artifactId>
  37. </dependency>
  38. <dependency>
  39.     <groupId>org.mybatis.spring.boot</groupId>
  40.     <artifactId>mybatis-spring-boot-starter</artifactId>
  41.     <version>2.1.4</version>
  42. </dependency>
  43. <dependency>
  44.     <groupId>org.projectlombok</groupId>
  45.     <artifactId>lombok</artifactId>
  46.     <version>1.18.22</version>
  47. </dependency>
  48. <dependency>
  49.     <groupId>org.thymeleaf</groupId>
  50.     <artifactId>thymeleaf-spring5</artifactId>
  51.     <version>3.0.11.RELEASE</version>
  52. </dependency>
  53. <dependency>
  54.     <groupId>org.thymeleaf.extras</groupId>
  55.     <artifactId>thymeleaf-extras-java8time</artifactId>
  56.     <version>3.0.4.RELEASE</version>
  57. </dependency>
  58. <dependency>
  59.     <groupId>com.github.theborakompanioni</groupId>
  60.     <artifactId>thymeleaf-extras-shiro</artifactId>
  61.     <version>2.0.0</version>
  62. </dependency>
复制代码
3.2.2 application.yml
  1. spring:
  2.   datasource:
  3.     username: root
  4.     password: 17585273765
  5.     url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8
  6.     driver-class-name: com.mysql.cj.jdbc.Driver
  7.     type: com.alibaba.druid.pool.DruidDataSource
  8.     #Spring Boot 默认是不注入这些属性值的,需要自己绑定
  9.     #druid 数据源专有配置
  10.     initialSize: 5
  11.     minIdle: 5
  12.     maxActive: 20
  13.     maxWait: 60000
  14.     timeBetweenEvictionRunsMillis: 60000
  15.     minEvictableIdleTimeMillis: 300000
  16.     validationQuery: SELECT 1 FROM DUAL
  17.     testWhileIdle: true
  18.     testOnBorrow: false
  19.     testOnReturn: false
  20.     poolPreparedStatements: true
  21.     #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
  22.     #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
  23.     #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
  24.     filters: stat,wall,log4j
  25.     maxPoolPreparedStatementPerConnectionSize: 20
  26.     useGlobalDataSourceStat: true
  27.     connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
  28.   thymeleaf:
  29.     cache: false
复制代码
3.2.3 静态资源

3.2.3.1 login.html
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.themeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>登录页面</title>
  6. </head>
  7. <body>
  8. <h1>登录</h1>
  9. <p th:text="${msg}" ></p>
  10. <form th:action="@{/login}">
  11.     <p>用户名: <input type="text" name="username"></p>
  12.     <p>密码: <input type="text" name="password"></p>
  13.     <p><input type="submit"></p>
  14. </form>
  15. </body>
  16. </html>
复制代码
3.2.3.2 index.html
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.themeleaf.org"
  3.       xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro" >
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>首页</title>
  7. </head>
  8. <body>
  9. <h1>首页</h1>
  10. <p th:if="${session.loginUser} == null" >
  11.     <a th:target="_blank" href="https://www.cnblogs.com/@{/toLogin}">登录</a>
  12. </p>
  13. <p th:text="${msg}"></p>
  14. <hr>
  15.     <a th:target="_blank" href="https://www.cnblogs.com/@{/user/add}">add</a>
  16.     <a th:target="_blank" href="https://www.cnblogs.com/@{/user/update}">update</a>
  17. </body>
  18. </html>
复制代码
3.2.3.3  add.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>添加页面</title>
  6. </head>
  7. <body>
  8. <h1>add</h1>
  9. </body>
  10. </html>
复制代码
3.2.3.4 update.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>修改页面</title>
  6. </head>
  7. <body>
  8. <h1>update</h1>
  9. </body>
  10. </html>
复制代码
3.2.4 根据用户名和密码查询用户信息


3.2.5 shiro配置信息

3.2.5.1 UserRealm 认证
  1. //自定义的 UserRealm  extends AuthorizingRealm
  2. public class UserRealm extends AuthorizingRealm {
  3.     @Autowired
  4.     UserService userService;
  5.     //授权
  6.     @Override
  7.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
  8.         System.out.println("执行了=>授权doGetAuthorizationInfo");
  9.         SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
  10.         //info.addStringPermission("user:add");  //给用户添加add权限
  11.         //拿到当前登录的这个对象
  12.         Subject subject = SecurityUtils.getSubject();
  13.         User currentUser  = (User)subject.getPrincipal();  //Shiro取出当前对象   拿到user对象
  14.         //设置当前用户的权限
  15.         info.addStringPermission(currentUser.getPerms());
  16.         return info;
  17.     }
  18.     //认证
  19.     @Override
  20.     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
  21.         System.out.println("执行了=>认证doGetAuthenticationInfo");
  22.         //用户名 密码  模拟 数据库中取
  23. //        String name = "root";
  24. //        String password = "123456";
  25.         UsernamePasswordToken userToken = (UsernamePasswordToken)token;
  26.         //连接真实数据库
  27.         User user = userService.queryUserByName(userToken.getUsername());
  28.         //模拟判断
  29. /*       if (!userToken.getUsername().equals(name)){
  30.             return null; //抛出异常 UnknownAccountException
  31.         }
  32.         //密码认证 shiro
  33.         return new SimpleAuthenticationInfo("",user.password,"");
  34. */
  35.         //数据库判断
  36.         if(user == null){ //没有这个人
  37.             return  null;
  38.         }
  39.         //获取session对象,保存user对象
  40.         Subject currentSubject = SecurityUtils.getSubject();
  41.         Session session = currentSubject.getSession();
  42.         session.setAttribute("loginUser",user);
  43.         //可以加密:md5  md5盐值加密
  44.         //密码认证 shiro
  45.         //第一个参数user是为了传递参数
  46.         return new SimpleAuthenticationInfo(user,user.getPwd(),"");
  47.     }
  48. }
复制代码
3.2.5.2 ShiroConfig 授权
  1. @Configuration
  2. public class ShiroConfig {
  3.     //ShiroFilterFactoryBean   工厂对象 第三步
  4.     @Bean
  5.     public ShiroFilterFactoryBean  getShiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
  6.         ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
  7.         //设置安全管理器
  8.         bean.setSecurityManager(defaultWebSecurityManager);
  9.         //添加shiro的内置过滤器
  10.         /*
  11.            anon: 无需认证就可以访问
  12.            authc: 必须认证; 才能访问
  13.            user: 必须拥有某个资源的权限才能访问
  14.            role: 拥有某个角色权限才能访问
  15.          */
  16.         //拦截
  17.         Map<String, String> filterMap = new LinkedHashMap<>();
  18.         //拦截路径
  19. //        filterMap.put("/user/add","authc");
  20. //        filterMap.put("/user/update","authc");
  21.         //授权 ,正常情况下,没有授权会跳到未授权页面
  22.         filterMap.put("/user/add","perms[user:add]"); //有add权限的user才可以访问/user/add
  23.         filterMap.put("/user/update","perms[user:update]");
  24.         bean.setFilterChainDefinitionMap(filterMap);
  25.         bean.setLoginUrl("/toLogin"); //设置登录的请求
  26.         //未授权页面
  27.         bean.setUnauthorizedUrl("/noauth");
  28.         return bean;
  29.     }
  30.     //DefaultWebSecurityManger  安全对象  第二步
  31.     @Bean("defaultWebSecurityManager")
  32.     public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
  33.         DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
  34.         //关联 userRealm
  35.         securityManager.setRealm(userRealm);
  36.         return securityManager;
  37.     }
  38.     //创建 realm 对象  ,需要自定义类  第一步
  39.     @Bean(name = "userRealm")
  40.     public UserRealm userRealm(){
  41.         return  new UserRealm();
  42.     }
  43.     //整合ShiroDialect:用来整合 Shiro
  44.     @Bean
  45.     public ShiroDialect getShiroDialect(){
  46.         return new ShiroDialect();
  47.     }
  48. }
复制代码
3.2.6 控制层
  1. @Controller
  2. public class MyController {
  3.     //index
  4.     @GetMapping({"/","/index"})
  5.     public String toIndex(Model model){
  6.         model.addAttribute("msg","hello,shiro");
  7.         return "index";
  8.     }
  9.     //add
  10.     @RequestMapping("/user/add")
  11.     public String add(){
  12.         return "user/add";
  13.     }
  14.     //update
  15.     @RequestMapping("/user/update")
  16.     public String update(){
  17.         return "user/update";
  18.     }
  19.     //去登录
  20.     @RequestMapping("/toLogin")
  21.     public String toLogin(){
  22.         return "login";
  23.     }
  24.     //登录
  25.     @RequestMapping("/login")
  26.     public String login( String username, String password,Model model){
  27.         //获取当前的用户
  28.         Subject subject = SecurityUtils.getSubject();
  29.         //封装用户的登陆数据
  30.         UsernamePasswordToken token = new UsernamePasswordToken(username, password);
  31.         try {
  32.             subject.login(token); //执行登陆方法,如果没有异常就说明Ok
  33.             return "index";  //登录成功,返回首页
  34.         }catch (UnknownAccountException e){ //用户名bu存在
  35.             model.addAttribute("msg","用户名错误");
  36.             return "login";
  37.         }catch (IncorrectCredentialsException e){  //密码不存在
  38.             model.addAttribute("msg","密码错误");
  39.             return "login";
  40.         }
  41.     }
  42.     //未授权
  43.     @RequestMapping("/noauth")
  44.     @ResponseBody
  45.     public String unauthorized(){
  46.         return "未经授权无法访问此页面!!!";
  47.     }
  48. }
复制代码
3.2.7 测试

3.2.7.1 没有权限


3.2.7.2 add 权限


3.2.7.3 update 权限


3.2.7.4 add +  update 权限






免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

干翻全岛蛙蛙

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表