zoukankan      html  css  js  c++  java
  • spring boot2.x集成spring security5与druid1.1.13(一)

    版本:

            spring boot 2.1.2.RELEASE

            druid-spring-boot-starter 1.1.13

    步骤:  

         一.maven        

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
      </parent>
    ...
    <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid-spring-boot-starter</artifactId>
          <version>1.1.13</version>
        </dependency>

      二、yml配置

    spring:
      profiles: dev
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/sina?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
        name: druidDataSource
        type: com.alibaba.druid.pool.DruidDataSource
       # driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: root
        druid:
          #初始化大小,最小,最大
          initial-size: 5
          min-idle: 5
          max-active: 20
          #配置获取连接等待超时的时间
          max-wait: 60000
          #配置多久检测一次,检测需要关闭空间连接,单位ms
          time-between-eviction-runs-millis: 60000
          # 配置一个池中最小生存的时间
          min-evictable-idle-time-millis: 300000
          #用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用
          validation-query: SELECT 1 FROM DUAL
          #建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
          test-while-idle: true
          #申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
          test-on-borrow: false
          #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
          test-on-return: false
          #要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100
          max-pool-prepared-statement-per-connection-size: 20
          #属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
          #监控统计用的filter:stat
          #日志用的filter:log4j
          #防御sql注入的filter:wall
          filters: stat,wall
          #合并多个DruidDataSource的监控数据
          use-global-data-source-stat: true
          #通过connectProperties属性打开mergeSql功能;慢sql记录
          connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
          # 配置监控服务器
          stat-view-servlet:
            login-username: admin
            login-password: 123456
            url-pattern: /druid/*
            #添加ip白名单
            allow: 127.0.0.1,192.168.0.5
            #黑名单,黑白名单有重复,黑优先级高
            #deny:
            #  禁用HTML页面上的“Reset All”功能
            reset-enable: false
            # 必须启用,要不会404
            enabled: true
          web-stat-filter:
            #添加过滤规则
            url-pattern: /*
            #忽略过滤格式
            exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico"
            enabled: true
          async-close-connection-enable: true
          aop-patterns: com.sswchat.service.*
          filter:
            stat:
              db-type: mysql
              log-slow-sql: true
              slow-sql-millis: 2000
              enabled: true
    
      jpa:
        show-sql: true
        hibernate:
          ddl-auto: create-drop

    我的环境在不加enable:true时访问http://127.0.0.1:8080/druid/login.html会报404

    三、Security的SecurityConfig配置文件

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/css/**", "/sign", "/druid/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() //.antMatchers("/css/**","/sign","/druid/**").permitAll()// 与/ css / **和/ index匹配的请求是完全可访问的 .antMatchers("/user/**").hasRole("USER")//与/ user / **匹配的请求要求用户进行身份验证,并且必须与USER角色相关联 .and().formLogin().loginPage("/login") .failureUrl("/login-error")// 使用自定义登录页面和失败URL启用基于表单的身份验证 .and().csrf().disable(); } }

    配置好后,如果只重写configure(HttpSecurity http),会出现submitlogin 403错误。

    ...
  • 相关阅读:
    2016"百度之星"
    codeforces 55 div2 C.Title 模拟
    codeforces 98 div2 C.History 水题
    codeforces 97 div2 C.Replacement 水题
    codeforces 200 div2 C. Rational Resistance 思路题
    bzoj 2226 LCMSum 欧拉函数
    hdu 1163 九余数定理
    51nod 1225 余数的和 数学
    bzoj 2818 gcd 线性欧拉函数
    Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学
  • 原文地址:https://www.cnblogs.com/javage/p/10380802.html
Copyright © 2011-2022 走看看