springboot 基础知识2

1.Shiro
  realms,users,roles,permissions
  Factory
  SecurityManager
  SecurityUtils.setSecurityManager(securityManager);
  
  //获取当前的用户对象Subject
  Subject current = SecurityUtils.getSubject();
  
  //判断当前用户是否被认证
  current.isAuthenticated()
  UsernamePassword  token
  token.setRememberMe(true);
  
  //执行登录操作
  current.login(token);
  
  current.getPrincipal
  hasRole   isPermitted
  
  //注销
  current.logout() 
  
2.swagger
  在项目中使用Swagger 需要Springbox
  swagger2、UI
  测试运行 http://localhost:8080/swagger-ui.html
  
3.配置Swagger
  Swagger的bean实例Docket;
  
4.spring boot2.6.1 继承swagger报错:
  swagger Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
  
  因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。
  解决办法,修改application.yaml
  spring.mvc.pathmatch.matching-strategy=ant_path_matcher
  
  AntPathMatcher进行的是纯字符串操作和比对
  而PathPattern则对于任何一个字符串的pattern最终都会被解析为若干段的PathElement,
  这些PathElement以链式结构连接起来用以表示该pattern,形成一个对象数据,
  
5./* 是拦截所有的文件夹,不包含子文件夹
  /** 是拦截所有的文件夹及里面的子文件夹
  
6.Swagger配置扫描jiek
  Docket.select()
  
7. return new Docket(DocumentationType.SWAGGER_2)
                  .apiInfo(apiInfo())
                  .select()
                  //RequestHandlerSelectors 配置要扫描接口的方式
                  //basePackage 指定要扫描的包
                  //any() 扫描全部
                  //none() 不扫描
                  //withClassAnnotation 扫描类上的注解,参数是一个注解的反射对象
                  //withMethodAnnotation 扫描方法上的注解
                  .apis(RequestHandlerSelectors.basePackage("com.tjut.controller"))
                  //paths 过滤什么路径
                  .paths(PathSelectors.ant("/tjut/**"))
                  .build();   //build工厂模式
                  
8.配置是否启动Swagger
   .enable(true)
   
9.希望Swagger在生产环境中使用,在发布的时候不使用
  判断是不是生产环境 : flag=false
  .enbale(flag)
  
  //设置要显示的Swagger环境
  Profiles profiles = Profiles.of("dev","test");
  //获取项目的环境
  //通过environment.acceptsProfiles判断是否处在自己设定的环境当中
  boolean flag = environment.acceptsProfiles(profiles);
  
10.配置API文档的分组
   .groupName("A")
   
   配置多个分组
   多个Docket实例即可
   
11.实体类配置
   @ApiModel("用户实体类")   用在实体类中
   @ApiModelProperty("密码")   用在实体类的属性上
   @ApiOperation("hello 控制类")   放在类的方法上
   @ApiParam("用户名")   给方法的参数加注释
   
   总结:
    我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息
    接口文档实时更新
    可以在线测试
    
12.异步任务        @Async  @EnableAsync
   邮件任务
   定时执行任务
   
13.定时执行任务
   TaskScheduler   任务调度者
   TaskExecutor   任务执行者
   
   @EnableScheduleing    开启定时功能的注解
   @scheduled   什么执行
   
   Cron 表达式
 
14.springboot + redis
   SpringBoot 操作数据 :spring-data  jpa  jdbc  mongodb  redis
   SpringData 也是和SpringBoot 齐名的项目
   说明: 在SpringBoot2.x 之后,原来使用的 jedis 被替换为lettuce
   jedis : 采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用 jedis pool 连接池! BIO
   lettuce: 采用netty ,实例可以在多个线程中进行共享,不存在线程不安全的情况,可以减少线程数据,更像 Nio 模式。 
   
   源码分析
   @Bean
   @ConditionalOnMissingBean(name="redisTemplate")    //我们可以自己定义一个redisTemplate来替换这个默认的
   public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
       //默认的RedisTemplate 没有过多的设置,redis 对象都是需要序列化的
       //两个泛型都是 object,object 的类型, 我们后使用需要强制转换 <String ,object>
       RedisTemplate<Object, Object> template = new RedisTemplate();
       template.setConnectionFactory(redisConnectionFactory);
       return template;
   }
   
   @Bean
   @ConditionalOnMissingBean     // 由于String 是redis中最常使用的类型,所以说单独提出来一个bean!
   public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
       StringRedisTemplate template = new StringRedisTemplate();
       template.setConnectionFactory(redisConnectionFactory)
       return template;
   }
   
   springboot整合测试
   1.导入依赖
   2.配置连接
   3.测试!
   
15.Dubbo
   Apache Dubbo 是一款高性能、轻量级的开源 Java RPC 框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡
                以及服务自动注册和发现。 
                
   Dubbo环境搭建
   
    zookeeper: hadoop、hive
    bin 目录:
           zkCli.cmd               验证是否安装成功
           zkServer.cmd         启动zookeeper服务
    添加zookeeper日志文件位置
       存放data(数据存储)和log(数据日志)
       
16.dataDir=./  临时数据存储的目录(可写相对路径)
   clientPort=2181   zookeeper的端口号
   
   ls /
   create -e /lei     创建
   get /lei     得到值
   
   dubbo-admin: 是一个监控管理后台,查看我们注册了哪些服务,哪些服务被消费了
   Dubbo: jar包
   
17.使用了Dubbo后尽量不要用Service注解
   服务应用名字
   dubbo.application.name=provide-server
   注册中心地址
   dubbo.registry.address=zookeeper://127.0.0.1:2181
   哪些服务要被注册
   dubbo.scan.base-packages=com.tjut.service
   
   依赖:
   dubo-spring-boot-starter
   zklient
   zookeeper依赖
   curator-recipes
   zookeeper
   排除slf4j-log4j12依赖
   
18.@Reference
   引用, Pom坐标,可以定义路径相同的接口名
   
   前提是:zookeeper要开启

19.druid开启监控模式

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    //后台监控 : web.xml ServletRegistrationBean
    //Springboot 内置了 servlet 容器,没有web.xml ,替代方法ServletRegistrationBean
    @Bean
    public ServletRegistrationBean a(){
        //模板是死的
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");

        //后台需要有人登录,账号密码配置
        HashMap<String ,String> initParameters = new HashMap<>();
        //增加配置
        initParameters.put("loginUsername","admin");  //登陆key是固定的 loginUsername loginPassword
        initParameters.put("loginPassword","123456");

        //允许谁可以访问
        initParameters.put("allow","");

        /*//禁止谁能访问
        initParameters.put("kua","192.168.1.2");*/

        bean.setInitParameters(initParameters);//设置初始化参数
        return bean;
    }

    //filter
    @Bean
        public FilterRegistrationBean webStatFilter(){
            FilterRegistrationBean bean = new FilterRegistrationBean();

            bean.setFilter(new WebStatFilter());
            //过滤请求
            Map<String,String> initParameters = new HashMap<>();

            //这些东西不进行统计
            initParameters.put("exclusions","*.js,*.css,/druid/*");
            bean.setInitParameters(initParameters);
            return bean;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值