狂神说java --springboot笔记

狂神说Java SpringBoot

狂神说SpringBoot01:Hello,World!

狂神说SpringBoot02:运行原理初探

狂神说SpringBoot03:yaml配置注入

狂神说SpringBoot04:JSR303数据校验及多环境切换

狂神说SpringBoot05:自动配置原理

狂神说SpringBoot06:自定义starter

狂神说SpringBoot07:整合JDBC

狂神说SpringBoot08:整合Druid

狂神说SpringBoot09:整合MyBatis

狂神说SpringBoot10:Web开发静态资源处理

狂神说SpringBoot11:Thymeleaf模板引擎

狂神说SpringBoot12:MVC自动配置原理

狂神说SpringBoot13:页面国际化

狂神说SpringBoot14:集成Swagger终极版

狂神说SpringBoot15:异步、定时、邮件任务

狂神说SpringBoot16:富文本编辑器

狂神说SpringBoot17:Dubbo和Zookeeper集成

狂神说SpringBoot18:集成SpringSecurity

一、运行原理初探

注解:

狂神说java   --springboot笔记

<span style="color:#000000"><code class="language-java"><span style="color:#5c6370">//获取所有配置</span>
<span style="color:#c678dd">protected</span> List<span style="color:#61aeee"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> <span style="color:#61aeee">getCandidateConfigurations</span><span style="color:#999999">(</span>AnnotationMetadata metadata<span style="color:#999999">,</span> AnnotationAttributes attributes<span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span>

 

获取候选配置:

<span style="color:#000000"><code class="language-java">    <span style="color:#c678dd">protected</span> List<span style="color:#61aeee"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> <span style="color:#61aeee">getCandidateConfigurations</span><span style="color:#999999">(</span>AnnotationMetadata metadata<span style="color:#999999">,</span> AnnotationAttributes attributes<span style="color:#999999">)</span> <span style="color:#999999">{</span>
        List<span style="color:#61aeee"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> configurations <span style="color:#669900">=</span> SpringFactoriesLoader<span style="color:#999999">.</span><span style="color:#61aeee">loadFactoryNames</span><span style="color:#999999">(</span><span style="color:#c678dd">this</span><span style="color:#999999">.</span><span style="color:#61aeee">getSpringFactoriesLoaderFactoryClass</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">,</span> <span style="color:#c678dd">this</span><span style="color:#999999">.</span><span style="color:#61aeee">getBeanClassLoader</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        Assert<span style="color:#999999">.</span><span style="color:#61aeee">notEmpty</span><span style="color:#999999">(</span>configurations<span style="color:#999999">,</span> <span style="color:#669900">"No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct."</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#c678dd">return</span> configurations<span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
</code></span>

二、JSR303数据校验及多环境切换

  1. 配置文件加载位置

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5Nr27xHV-1594913883189)(D:\我\MyBlog\狂神说Java SpringBoot.assets\image-20200709213144157.png)]

​ file : 文件路径,就是项目路径

​ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MmY445bN-狂神说java   --springboot笔记

三、SpringBoot Web开发

自动装配:

SpringBoot到底帮我们配置了什么?我们能不能进行修改?能修改哪些东西?能不能扩展?

  • xxxAutoConfiguration…向容器中自动配置组件
  • xxxProperties:自动配置类,装配配置文件中自定义的一些内容

要解决的问题:

  • 导入静态资源…

  • 首页

  • jsp, 模板引擎Thymeleaf

    thymeleaf依赖

  • 装配扩展SpringMVC

  • 增删改查

  • 拦截器

  • 国际化

四、员工管理系统

  1. 首页配置
    1. 注意点,所有页面的静态资源都需要使用thymeleaf接管;(导入thymeleaf依赖)
    2. url: @{}
  2. 页面国际化
    1. 我们需要配置i18n文件
    2. 我们如果需要在项目中进行按钮自动切换,我们需要自定义一个组件LocaleResolver
    3. 记得将自己写的组件配置到spring容器@Bean
    4. #{}

五、整合MyBatis

整合包

mybatis-spring-boot-starter

  1. 导入包

    <span style="color:#000000"><code class="language-xml"><span style="color:#5c6370"><!--引入mybatis,这是Mybatis官方提供的适配SpringBoot的,而不是SpringBoot自己的--></span>
    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.mybatis.spring.boot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>mybatis-spring-boot-starter<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>2.1.1<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    </code></span>
  2. 配置yml文件

    application.yml

    <span style="color:#000000"><code class="language-yml"><span style="color:#5c6370"># 配置spring自带的数据源</span>
    <span style="color:#c678dd">spring</span><span style="color:#999999">:</span>
      <span style="color:#c678dd">datasource</span><span style="color:#999999">:</span>
        <span style="color:#c678dd">username</span><span style="color:#999999">:</span> root
        <span style="color:#c678dd">password</span><span style="color:#999999">:</span> root
        <span style="color:#c678dd">url</span><span style="color:#999999">:</span> jdbc<span style="color:#999999">:</span>mysql<span style="color:#999999">:</span>//localhost<span style="color:#999999">:</span>3306/mybatis<span style="color:#999999">?</span>userSSL=true<span style="color:#ee9900">&useUnicode</span>=true<span style="color:#ee9900">&characterEncoding</span>=UTF<span style="color:#999999">-</span>8<span style="color:#ee9900">&serverTimezone</span>=UTC
        <span style="color:#c678dd">driver-class-name</span><span style="color:#999999">:</span> com.mysql.cj.jdbc.Driver
    
    <span style="color:#5c6370"># 整合mybatis</span>
    <span style="color:#c678dd">mybatis</span><span style="color:#999999">:</span>
      <span style="color:#5c6370"># 别名</span>
      <span style="color:#c678dd">type-aliases-package</span><span style="color:#999999">:</span> com.kuang.pojo
      <span style="color:#5c6370"># mapper文件位置</span>
      <span style="color:#c678dd">mapper-locations</span><span style="color:#999999">:</span> classpath<span style="color:#999999">:</span>mybatis/mapper/*.xml
    </code></span>
  3. mybatis配置

    • User

      <span style="color:#000000"><code class="language-java"><span style="color:#999999">@Data</span>
      <span style="color:#999999">@AllArgsConstructor</span>
      <span style="color:#999999">@NoArgsConstructor</span>
      <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> User <span style="color:#999999">{</span>
          <span style="color:#c678dd">private</span> <span style="color:#c678dd">int</span> id<span style="color:#999999">;</span>
          <span style="color:#c678dd">private</span> String name<span style="color:#999999">;</span>
          <span style="color:#c678dd">private</span> String password<span style="color:#999999">;</span>
      <span style="color:#999999">}</span>
      </code></span>
    • UserMapper接口

      <span style="color:#000000"><code class="language-java"><span style="color:#999999">@Repository</span>
      <span style="color:#999999">@Mapper</span>
      <span style="color:#c678dd">public</span> <span style="color:#c678dd">interface</span> UserMapper <span style="color:#999999">{</span>
          <span style="color:#c678dd">public</span> User <span style="color:#61aeee">queryUserByName</span><span style="color:#999999">(</span>String name<span style="color:#999999">)</span><span style="color:#999999">;</span>  
      <span style="color:#999999">}</span>
      </code></span>
    • UserMapper.xml配置文件

      <span style="color:#000000"><code class="language-xml"><span style="color:#5c6370"><!--namespace=绑定一个指定的Dao/Mapper接口--></span>
      <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>mapper</span> <span style="color:#d19a66">namespace</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>com.kuang.mapper.UserMapper<span style="color:#999999">"</span></span><span style="color:#999999">></span></span>
          <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>select</span> <span style="color:#d19a66">id</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>getUserList<span style="color:#999999">"</span></span> <span style="color:#d19a66">resultType</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>com.kuang.pojo.User<span style="color:#999999">"</span></span> <span style="color:#d19a66">parameterType</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>String<span style="color:#999999">"</span></span><span style="color:#999999">></span></span>
          select * from USER where name = #{name}
        	<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>select</span><span style="color:#999999">></span></span>
      <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>mapper</span><span style="color:#999999">></span></span>
      </code></span>
  4. 编写sql

  5. service层调用dao层

    • UserService 接口

      <span style="color:#000000"><code class="language-java"><span style="color:#c678dd">public</span> <span style="color:#c678dd">interface</span> UserService <span style="color:#999999">{</span>
          <span style="color:#c678dd">public</span> User <span style="color:#61aeee">queryUserByName</span><span style="color:#999999">(</span>String name<span style="color:#999999">)</span><span style="color:#999999">;</span>
      <span style="color:#999999">}</span>
      </code></span>
    • UserServiceImpl实现类

      <span style="color:#000000"><code class="language-java"><span style="color:#999999">@Service</span>
      <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> UserServiceImpl <span style="color:#c678dd">implements</span> UserService<span style="color:#999999">{</span>
          <span style="color:#999999">@Autowired</span>
          UserMapper mapper<span style="color:#999999">;</span>
          <span style="color:#c678dd">public</span> User <span style="color:#61aeee">queryUserByName</span><span style="color:#999999">(</span>String name<span style="color:#999999">)</span> <span style="color:#999999">{</span>
              User user <span style="color:#669900">=</span> mapper<span style="color:#999999">.</span><span style="color:#61aeee">queryUserByName</span><span style="color:#999999">(</span>name<span style="color:#999999">)</span><span style="color:#999999">;</span>
              <span style="color:#c678dd">return</span> user<span style="color:#999999">;</span>
          <span style="color:#999999">}</span>
      <span style="color:#999999">}</span>
      </code></span>
  6. controller调用service层

    <span style="color:#000000"><code class="language-java"><span style="color:#999999">@Autowired</span>
    UserServiceImpl userService<span style="color:#999999">;</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">mian</span><span style="color:#999999">(</span>String args<span style="color:#999999">[</span><span style="color:#999999">]</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        User user <span style="color:#669900">=</span> userService<span style="color:#999999">.</span><span style="color:#61aeee">queryUserByName</span><span style="color:#999999">(</span><span style="color:#669900">"dog"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
    </code></span>

六、SpringSecurity

  1. 引入 Spring Security 模块

  2. 编写 Spring Security 配置类

    参考官网:https://spring.io/projects/spring-security

  3. 编写基础配置类

    • 定制请求的授权规则
    • 定义认证规则
    <span style="color:#000000"><code class="language-java"><span style="color:#5c6370">//AOP : 拦截器</span>
    <span style="color:#999999">@EnableWebSecurity</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> SecurityConfig <span style="color:#c678dd">extends</span> WebSecurityConfigurerAdapter <span style="color:#999999">{</span>
    
        <span style="color:#5c6370">// 链式编程</span>
        <span style="color:#999999">@Override</span>
        <span style="color:#5c6370">//授权</span>
        <span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">configure</span><span style="color:#999999">(</span>HttpSecurity http<span style="color:#999999">)</span> <span style="color:#c678dd">throws</span> Exception <span style="color:#999999">{</span>
            <span style="color:#5c6370">//首页所有人可以访问,功能页只有对应有权限的人才能访问</span>
            <span style="color:#5c6370">//请求授权的规则</span>
            http<span style="color:#999999">.</span><span style="color:#61aeee">authorizeRequests</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">antMatchers</span><span style="color:#999999">(</span><span style="color:#669900">"/"</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">permitAll</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">antMatchers</span><span style="color:#999999">(</span><span style="color:#669900">"/level1/**"</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">hasRole</span><span style="color:#999999">(</span><span style="color:#669900">"vip1"</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">antMatchers</span><span style="color:#999999">(</span><span style="color:#669900">"/level2/**"</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">hasRole</span><span style="color:#999999">(</span><span style="color:#669900">"vip2"</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">antMatchers</span><span style="color:#999999">(</span><span style="color:#669900">"/level3/**"</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">hasRole</span><span style="color:#999999">(</span><span style="color:#669900">"vip3"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            <span style="color:#5c6370">//没有权限默认会到登录页面,需要开启登录的页面</span>
            http<span style="color:#999999">.</span><span style="color:#61aeee">formLogin</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">loginPage</span><span style="color:#999999">(</span><span style="color:#669900">"/toLogin"</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">usernameParameter</span><span style="color:#999999">(</span><span style="color:#669900">"username"</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">passwordParameter</span><span style="color:#999999">(</span><span style="color:#669900">"password"</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">loginProcessingUrl</span><span style="color:#999999">(</span><span style="color:#669900">"/login"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            http<span style="color:#999999">.</span><span style="color:#61aeee">csrf</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">disable</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span><span style="color:#669900">/</span><span style="color:#669900">/</span>关闭csrf功能<span style="color:#669900">:</span>跨站请求伪造<span style="color:#999999">,</span>默认只能通过post方式提交logout请求
            <span style="color:#669900">/</span><span style="color:#669900">/</span> 开启注销功能
            http<span style="color:#999999">.</span><span style="color:#61aeee">logout</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">logoutSuccessUrl</span><span style="color:#999999">(</span><span style="color:#669900">"/"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#669900">/</span><span style="color:#669900">/</span>开启记住我功能
            http<span style="color:#999999">.</span><span style="color:#61aeee">rememberMe</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">rememberMeParameter</span><span style="color:#999999">(</span><span style="color:#669900">"rememberme"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    
        <span style="color:#669900">/</span><span style="color:#669900">/</span>认证
        <span style="color:#999999">@Override</span>
        <span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">configure</span><span style="color:#999999">(</span>AuthenticationManagerBuilder auth<span style="color:#999999">)</span> <span style="color:#c678dd">throws</span> Exception <span style="color:#999999">{</span>
       <span style="color:#669900">/</span><span style="color:#669900">/</span>在内存中定义,也可以在jdbc中去拿<span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span>
       <span style="color:#669900">/</span><span style="color:#669900">/</span>Spring security <span style="color:#98c379">5.0</span>中新增了多种加密方式,也改变了密码的格式。
       <span style="color:#669900">/</span><span style="color:#669900">/</span>要想我们的项目还能够正常登陆,需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密
       <span style="color:#669900">/</span><span style="color:#669900">/</span>spring security 官方推荐的是使用bcrypt加密方式。
    
            auth<span style="color:#999999">.</span><span style="color:#61aeee">inMemoryAuthentication</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">passwordEncoder</span><span style="color:#999999">(</span><span style="color:#c678dd">new</span> BCryptPasswordEncoder<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">withUser</span><span style="color:#999999">(</span><span style="color:#669900">"kuangshen"</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">password</span><span style="color:#999999">(</span><span style="color:#c678dd">new</span> BCryptPasswordEncoder<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">encode</span><span style="color:#999999">(</span><span style="color:#669900">"123456"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">roles</span><span style="color:#999999">(</span><span style="color:#669900">"vip2"</span><span style="color:#999999">,</span><span style="color:#669900">"vip3"</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">and</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">withUser</span><span style="color:#999999">(</span><span style="color:#669900">"admin"</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">password</span><span style="color:#999999">(</span><span style="color:#c678dd">new</span> BCryptPasswordEncoder<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">encode</span><span style="color:#999999">(</span><span style="color:#669900">"123456"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">roles</span><span style="color:#999999">(</span><span style="color:#669900">"vip1"</span><span style="color:#999999">,</span><span style="color:#669900">"vip2"</span><span style="color:#999999">,</span><span style="color:#669900">"vip3"</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">and</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
                    <span style="color:#999999">.</span><span style="color:#61aeee">withUser</span><span style="color:#999999">(</span><span style="color:#669900">"guest"</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">password</span><span style="color:#999999">(</span><span style="color:#c678dd">new</span> BCryptPasswordEncoder<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">encode</span><span style="color:#999999">(</span><span style="color:#669900">"123456"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">roles</span><span style="color:#999999">(</span><span style="color:#669900">"vip1"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>
    </code></span>

七、Shiro

  • Subject 用户
  • SecurityManager 管理用户
  • Realm 连接数据
    狂神说java   --springboot笔记
    简单实验:
  1. 导入依赖

    <span style="color:#000000"><code class="language-xml"><span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.apache.shiro<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>shiro-spring<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.5.3<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    </code></span>
  2. 编写Shiro配置类

    <span style="color:#000000"><code class="language-java"><span style="color:#999999">@Configuration</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> ShrioConfig <span style="color:#999999">{</span>
        <span style="color:#5c6370">//ShiroFilterFactoryBean : Step3</span>
        <span style="color:#999999">@Bean</span>
        <span style="color:#c678dd">public</span> ShiroFilterFactoryBean <span style="color:#61aeee">getShrioFilterFactoryBean</span><span style="color:#999999">(</span><span style="color:#999999">@Qualifier</span><span style="color:#999999">(</span><span style="color:#669900">"securityManager"</span><span style="color:#999999">)</span> DefaultWebSecurityManager defaultWebSecurityManager<span style="color:#999999">)</span><span style="color:#999999">{</span>
            ShiroFilterFactoryBean bean <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> ShiroFilterFactoryBean<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#5c6370">//设置安全管理器</span>
            bean<span style="color:#999999">.</span><span style="color:#61aeee">setSecurityManager</span><span style="color:#999999">(</span>defaultWebSecurityManager<span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#c678dd">return</span> bean<span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
        
        <span style="color:#5c6370">//DefaultWebSecurityManager : Step2</span>
        <span style="color:#999999">@Bean</span><span style="color:#999999">(</span><span style="color:#669900">"securityManager"</span><span style="color:#999999">)</span>
        <span style="color:#c678dd">public</span> DefaultWebSecurityManager <span style="color:#61aeee">getDefaultWebSecurityManager</span><span style="color:#999999">(</span><span style="color:#999999">@Qualifier</span><span style="color:#999999">(</span><span style="color:#669900">"userRealm"</span><span style="color:#999999">)</span> UserRealm userRealm<span style="color:#999999">)</span><span style="color:#999999">{</span>
            DefaultWebSecurityManager securityManager <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> DefaultWebSecurityManager<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#5c6370">//关联userRealm</span>
            securityManager<span style="color:#999999">.</span><span style="color:#61aeee">setRealm</span><span style="color:#999999">(</span>userRealm<span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#c678dd">return</span> securityManager<span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
        
        <span style="color:#5c6370">//创建 realm 对象, 需要自定义类:Step1</span>
        <span style="color:#999999">@Bean</span>
        <span style="color:#c678dd">public</span> UserRealm <span style="color:#61aeee">userRealm</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
            <span style="color:#c678dd">return</span> <span style="color:#c678dd">new</span> UserRealm<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>
    </code></span>
  3. 自定义UserRealm

    <span style="color:#000000"><code class="language-java"><span style="color:#5c6370">//自定义的 UserRealm</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> UserRealm <span style="color:#c678dd">extends</span> AuthorizingRealm <span style="color:#999999">{</span>
    
        <span style="color:#5c6370">//授权</span>
        <span style="color:#999999">@Override</span>
        <span style="color:#c678dd">protected</span> AuthorizationInfo <span style="color:#61aeee">doGetAuthorizationInfo</span><span style="color:#999999">(</span>PrincipalCollection principalCollection<span style="color:#999999">)</span> <span style="color:#999999">{</span>
            <span style="color:#c678dd">return</span> null<span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    
        <span style="color:#5c6370">//认证</span>
        <span style="color:#999999">@Override</span>
        <span style="color:#c678dd">protected</span> AuthenticationInfo <span style="color:#61aeee">doGetAuthenticationInfo</span><span style="color:#999999">(</span>AuthenticationToken authenticationToken<span style="color:#999999">)</span> <span style="color:#c678dd">throws</span> AuthenticationException <span style="color:#999999">{</span>
            <span style="color:#c678dd">return</span> null<span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>
    </code></span>

一个小Demo:

  1. 导入依赖

    • springboot-mybatis整合
    • shiro-thymelea整合
    <span style="color:#000000"><code class="language-xml"><span style="color:#5c6370"><?xml version="1.0" encoding="UTF-8"?></span>
    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>project</span> <span style="color:#d19a66">xmlns</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>http://maven.apache.org/POM/4.0.0<span style="color:#999999">"</span></span> <span style="color:#d19a66">xmlns:xsi</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>http://www.w3.org/2001/XMLSchema-instance<span style="color:#999999">"</span></span>
             <span style="color:#d19a66">xsi:schemaLocation</span><span style="color:#669900"><span style="color:#999999">=</span><span style="color:#999999">"</span>http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd<span style="color:#999999">"</span></span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>modelVersion</span><span style="color:#999999">></span></span>4.0.0<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>modelVersion</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>parent</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-parent<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>2.3.1.RELEASE<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>relativePath</span><span style="color:#999999">/></span></span> <span style="color:#5c6370"><!-- lookup parent from repository --></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>parent</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>com.kuang<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>shiro-springboot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>0.0.1-SNAPSHOT<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>name</span><span style="color:#999999">></span></span>shiro-springboot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>name</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>description</span><span style="color:#999999">></span></span>Demo project for Spring Boot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>description</span><span style="color:#999999">></span></span>
    
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>properties</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>java.version</span><span style="color:#999999">></span></span>1.8<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>java.version</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>properties</span><span style="color:#999999">></span></span>
    
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependencies</span><span style="color:#999999">></span></span>
            <span style="color:#5c6370"><!--shiro-thymeleaf整合--></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>com.github.theborakompanioni<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>thymeleaf-extras-shiro<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>2.0.0<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>mysql<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>mysql-connector-java<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>8.0.20<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
            <span style="color:#5c6370"><!-- https://mvnrepository.com/artifact/log4j/log4j --></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>log4j<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>log4j<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.2.17<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>com.alibaba<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>druid<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.1.22<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
            <span style="color:#5c6370"><!--引入mybatis,这是Mybatis官方提供的适配SpringBoot的,而不是SpringBoot自己的--></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.mybatis.spring.boot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>mybatis-spring-boot-starter<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>2.1.1<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.projectlombok<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>lombok<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.18.12<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.apache.shiro<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>shiro-spring<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>1.5.3<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-thymeleaf<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-web<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-test<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>scope</span><span style="color:#999999">></span></span>test<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>scope</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>exclusions</span><span style="color:#999999">></span></span>
                    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>exclusion</span><span style="color:#999999">></span></span>
                        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.junit.vintage<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>junit-vintage-engine<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
                    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>exclusion</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>exclusions</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependencies</span><span style="color:#999999">></span></span>
    
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>build</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>plugins</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>plugin</span><span style="color:#999999">></span></span>
                    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
                    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-maven-plugin<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
                <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>plugin</span><span style="color:#999999">></span></span>
            <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>plugins</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>build</span><span style="color:#999999">></span></span>
    
    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>project</span><span style="color:#999999">></span></span>
    </code></span>
  2. 整合MyBatis

    • 编写实体类
    • 编写mapper接口、mapper.xml、application.yml配置mybatis(别名,mapper.xml文件位置)
    • 编写service接口,serviceImpl类
      狂神说java   --springboot笔记
  3. 编写Shiro配置类

    <span style="color:#000000"><code class="language-java"><span style="color:#999999">@Configuration</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> ShrioConfig <span style="color:#999999">{</span>
    
        <span style="color:#5c6370">//ShiroFilterFactoryBean : Step3</span>
        <span style="color:#999999">@Bean</span>
        <span style="color:#c678dd">public</span> ShiroFilterFactoryBean <span style="color:#61aeee">getShrioFilterFactoryBean</span><span style="color:#999999">(</span><span style="color:#999999">@Qualifier</span><span style="color:#999999">(</span><span style="color:#669900">"securityManager"</span><span style="color:#999999">)</span> DefaultWebSecurityManager defaultWebSecurityManager<span style="color:#999999">)</span><span style="color:#999999">{</span>
            ShiroFilterFactoryBean bean <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> ShiroFilterFactoryBean<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#5c6370">//设置安全管理器</span>
            bean<span style="color:#999999">.</span><span style="color:#61aeee">setSecurityManager</span><span style="color:#999999">(</span>defaultWebSecurityManager<span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            <span style="color:#5c6370">//添加shiro的内置过滤器</span>
            <span style="color:#5c6370">/*
                anno: 无需认证就可以访问
                authc: 必须认证了才可以访问
                user: 必须拥有 记住我 功能才能用
                perms: 拥有对某个资源的权限才能访问
                role: 拥有某个角色权限才能访问
            */</span>
    
            Map<span style="color:#61aeee"><span style="color:#999999"><</span>String<span style="color:#999999">,</span> String<span style="color:#999999">></span></span> filterMap <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> LinkedHashMap<span style="color:#669900"><</span><span style="color:#669900">></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            <span style="color:#5c6370">//用户授权,正常情况下没有授权会跳转到授权页面</span>
            filterMap<span style="color:#999999">.</span><span style="color:#61aeee">put</span><span style="color:#999999">(</span><span style="color:#669900">"/user/add"</span><span style="color:#999999">,</span><span style="color:#669900">"perms[user:add]"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            filterMap<span style="color:#999999">.</span><span style="color:#61aeee">put</span><span style="color:#999999">(</span><span style="color:#669900">"/user/update"</span><span style="color:#999999">,</span><span style="color:#669900">"perms[user:update]"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            <span style="color:#5c6370">//拦截</span>
            <span style="color:#5c6370">//filterMap.put("/user/add","authc");</span>
            <span style="color:#5c6370">//filterMap.put("/user/update","authc");</span>
            filterMap<span style="color:#999999">.</span><span style="color:#61aeee">put</span><span style="color:#999999">(</span><span style="color:#669900">"/user/*"</span><span style="color:#999999">,</span><span style="color:#669900">"authc"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            <span style="color:#5c6370">//设置登录请求</span>
            bean<span style="color:#999999">.</span><span style="color:#61aeee">setLoginUrl</span><span style="color:#999999">(</span><span style="color:#669900">"/toLogin"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#669900">/</span><span style="color:#669900">/</span>设置未授权页面
            bean<span style="color:#999999">.</span><span style="color:#61aeee">setUnauthorizedUrl</span><span style="color:#999999">(</span><span style="color:#669900">"/noauth"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            bean<span style="color:#999999">.</span><span style="color:#61aeee">setFilterChainDefinitionMap</span><span style="color:#999999">(</span>filterMap<span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#c678dd">return</span> bean<span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    
        <span style="color:#669900">/</span><span style="color:#669900">/</span>DefaultWebSecurityManager <span style="color:#669900">:</span> Step2
        <span style="color:#999999">@Bean</span><span style="color:#999999">(</span><span style="color:#669900">"securityManager"</span><span style="color:#999999">)</span>
        <span style="color:#c678dd">public</span> DefaultWebSecurityManager <span style="color:#61aeee">getDefaultWebSecurityManager</span><span style="color:#999999">(</span><span style="color:#999999">@Qualifier</span><span style="color:#999999">(</span><span style="color:#669900">"userRealm"</span><span style="color:#999999">)</span> UserRealm userRealm<span style="color:#999999">)</span><span style="color:#999999">{</span>
            DefaultWebSecurityManager securityManager <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> DefaultWebSecurityManager<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#669900">/</span><span style="color:#669900">/</span>关联userRealm
            securityManager<span style="color:#999999">.</span><span style="color:#61aeee">setRealm</span><span style="color:#999999">(</span>userRealm<span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#c678dd">return</span> securityManager<span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    
        <span style="color:#669900">/</span><span style="color:#669900">/</span>创建 realm 对象<span style="color:#999999">,</span> 需要自定义类:Step1
        <span style="color:#999999">@Bean</span>
        <span style="color:#c678dd">public</span> UserRealm <span style="color:#61aeee">userRealm</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
            <span style="color:#c678dd">return</span> <span style="color:#c678dd">new</span> UserRealm<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    
        <span style="color:#669900">/</span><span style="color:#669900">/</span>整合 ShiroDialect<span style="color:#669900">:</span>用来整合shiro thymeleaf
        <span style="color:#999999">@Bean</span>
        <span style="color:#c678dd">public</span> ShiroDialect <span style="color:#61aeee">getShiroDialect</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
            <span style="color:#c678dd">return</span> <span style="color:#c678dd">new</span> ShiroDialect<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>
    </code></span>
  4. 自定义UserRealm

    <span style="color:#000000"><code class="language-java"><span style="color:#5c6370">//自定义的 UserRealm</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> UserRealm <span style="color:#c678dd">extends</span> AuthorizingRealm <span style="color:#999999">{</span>
    
        <span style="color:#999999">@Autowired</span>
        UserServiceImpl userService<span style="color:#999999">;</span>
    
        <span style="color:#5c6370">//授权</span>
        <span style="color:#999999">@Override</span>
        <span style="color:#c678dd">protected</span> AuthorizationInfo <span style="color:#61aeee">doGetAuthorizationInfo</span><span style="color:#999999">(</span>PrincipalCollection principalCollection<span style="color:#999999">)</span> <span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">"执行了 => doGetAuthorizationInfo"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            SimpleAuthorizationInfo info <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> SimpleAuthorizationInfo<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#5c6370">// 拿到当前登录的这个对象</span>
            Subject subject <span style="color:#669900">=</span> SecurityUtils<span style="color:#999999">.</span><span style="color:#61aeee">getSubject</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#5c6370">// 拿到User对象</span>
            User currentUser <span style="color:#669900">=</span> <span style="color:#999999">(</span>User<span style="color:#999999">)</span> subject<span style="color:#999999">.</span><span style="color:#61aeee">getPrincipal</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#5c6370">// 设置当前用户的权限</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span>currentUser<span style="color:#999999">.</span><span style="color:#61aeee">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#669900">+</span> <span style="color:#669900">"的权限为 "</span> <span style="color:#669900">+</span> currentUser<span style="color:#999999">.</span><span style="color:#61aeee">getPerms</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            info<span style="color:#999999">.</span><span style="color:#61aeee">addStringPermission</span><span style="color:#999999">(</span>currentUser<span style="color:#999999">.</span><span style="color:#61aeee">getPerms</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            <span style="color:#c678dd">return</span> info<span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    
        <span style="color:#5c6370">//认证</span>
        <span style="color:#999999">@Override</span>
        <span style="color:#c678dd">protected</span> AuthenticationInfo <span style="color:#61aeee">doGetAuthenticationInfo</span><span style="color:#999999">(</span>AuthenticationToken authenticationToken<span style="color:#999999">)</span> <span style="color:#c678dd">throws</span> AuthenticationException <span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">"执行了 => 认证AuthenticationToken"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            UsernamePasswordToken userToken <span style="color:#669900">=</span> <span style="color:#999999">(</span>UsernamePasswordToken<span style="color:#999999">)</span> authenticationToken<span style="color:#999999">;</span>
            <span style="color:#5c6370">//连接真实的数据库</span>
            User user <span style="color:#669900">=</span> userService<span style="color:#999999">.</span><span style="color:#61aeee">queryUserByName</span><span style="color:#999999">(</span>userToken<span style="color:#999999">.</span><span style="color:#61aeee">getUsername</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#c678dd">if</span><span style="color:#999999">(</span>user <span style="color:#669900">==</span> null<span style="color:#999999">)</span><span style="color:#999999">{</span>
                <span style="color:#5c6370">//没有这个人</span>
                <span style="color:#c678dd">return</span> null<span style="color:#999999">;</span> <span style="color:#5c6370">//抛出异常 UnknownAccountException</span>
            <span style="color:#999999">}</span>
    
            <span style="color:#5c6370">// 登录成功 将用户信息存入session</span>
            Subject currentSubject <span style="color:#669900">=</span> SecurityUtils<span style="color:#999999">.</span><span style="color:#61aeee">getSubject</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            Session session <span style="color:#669900">=</span> currentSubject<span style="color:#999999">.</span><span style="color:#61aeee">getSession</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            session<span style="color:#999999">.</span><span style="color:#61aeee">setAttribute</span><span style="color:#999999">(</span><span style="color:#669900">"loginUser"</span><span style="color:#999999">,</span>user<span style="color:#999999">.</span><span style="color:#61aeee">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            <span style="color:#5c6370">// 密码认证,shiro做</span>
            <span style="color:#c678dd">return</span> <span style="color:#c678dd">new</span> SimpleAuthenticationInfo<span style="color:#999999">(</span>user<span style="color:#999999">,</span>user<span style="color:#999999">.</span><span style="color:#61aeee">getPassword</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">,</span><span style="color:#669900">""</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>
    </code></span>

八、Swagger

学习目标:

  • 了解Swagger的作用和概念
  • 了解前后端分离
  • 在SpringBoot中集成Swagger

一、 Swagger简介

前后端分离时代:

  • 后端:后端控制层,服务层,数据访问层
  • 前端:前端控制层,视图层

Swagger :

  • 号称世界上最流行的API框架;
  • RestFul Api文档在线自动生成工具 => Api文档与Api定义同步更新
  • 直接运行,可以在线测试API接口
  • 支持多种语言:(Java , Php…)

在项目中使用Swagger需要Springbox

  • swagger2
  • ui

二、SpringBoot集成Swagger

  1. 新建一个SpringBoot - web 工程

  2. 导入相关依赖

    <span style="color:#000000"><code class="language-xml"><span style="color:#5c6370"><!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --></span>
    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>io.springfox<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>springfox-swagger2<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>2.9.2<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    <span style="color:#5c6370"><!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --></span>
    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>io.springfox<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>springfox-swagger-ui<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>2.9.2<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    
    </code></span>
  3. 编写一个Hello工程

  4. 配置Swagger ==> Config

    <span style="color:#000000"><code class="language-java"><span style="color:#999999">@Configuration</span>
    <span style="color:#999999">@EnableSwagger2</span> <span style="color:#5c6370">//开启Swagger</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> SwaggerConfig <span style="color:#999999">{</span>
      
    <span style="color:#999999">}</span>
    </code></span>
  5. 测试运行:http://localhost:8080/swagger-ui.html

三、 配置Swagger信息

<span style="color:#000000"><code class="language-java"><span style="color:#999999">@Configuration</span>
<span style="color:#999999">@EnableSwagger2</span> <span style="color:#5c6370">//开启Swagger</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> SwaggerConfig <span style="color:#999999">{</span>
    <span style="color:#5c6370">//配置了Swagger 的Docket的bean实例</span>
    <span style="color:#999999">@Bean</span>
    <span style="color:#c678dd">public</span> Docket <span style="color:#61aeee">docket</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        <span style="color:#c678dd">return</span> <span style="color:#c678dd">new</span> Docket<span style="color:#999999">(</span>DocumentationType<span style="color:#999999">.</span>SWAGGER_2<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">apiInfo</span><span style="color:#999999">(</span><span style="color:#61aeee">apiInfo</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
    
    <span style="color:#5c6370">//配置Swagger信息=apiInfo</span>
    <span style="color:#c678dd">public</span> ApiInfo <span style="color:#61aeee">apiInfo</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
        <span style="color:#5c6370">//作者信息</span>
        Contact contact <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> Contact<span style="color:#999999">(</span><span style="color:#669900">"Daniel"</span><span style="color:#999999">,</span><span style="color:#669900">"https://www.baidu.com"</span><span style="color:#999999">,</span><span style="color:#669900">"denglianqing@qq.com"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#c678dd">return</span> <span style="color:#c678dd">new</span> ApiInfo<span style="color:#999999">(</span><span style="color:#669900">"Dainel的SwaggerAPI文档"</span><span style="color:#999999">,</span>
                <span style="color:#669900">"天道酬勤"</span><span style="color:#999999">,</span>
                <span style="color:#669900">"v1.0"</span><span style="color:#999999">,</span>
                <span style="color:#669900">"urn:tos"</span><span style="color:#999999">,</span>
                contact<span style="color:#999999">,</span> <span style="color:#669900">"Apache 2.0"</span><span style="color:#999999">,</span>
                <span style="color:#669900">"http://www.apache.org/licenses/LICENSE-2.0"</span><span style="color:#999999">,</span>
                <span style="color:#c678dd">new</span> ArrayList<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>

四、 Swagger配置扫描接口

Docket.select()

<span style="color:#000000"><code class="language-java"><span style="color:#5c6370">//配置了Swagger 的Docket的bean实例</span>
<span style="color:#999999">@Bean</span>
<span style="color:#c678dd">public</span> Docket <span style="color:#61aeee">docket</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
    <span style="color:#c678dd">return</span> <span style="color:#c678dd">new</span> Docket<span style="color:#999999">(</span>DocumentationType<span style="color:#999999">.</span>SWAGGER_2<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#61aeee">apiInfo</span><span style="color:#999999">(</span><span style="color:#61aeee">apiInfo</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
        <span style="color:#999999">.</span><span style="color:#61aeee">select</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
        <span style="color:#5c6370">//RequestHandlerSelectors,配置要扫描接口的方式</span>
        <span style="color:#5c6370">//basePackage,指定要扫描的包</span>
        <span style="color:#5c6370">//any(): 扫描全部</span>
        <span style="color:#999999">.</span><span style="color:#61aeee">apis</span><span style="color:#999999">(</span>RequestHandlerSelectors<span style="color:#999999">.</span><span style="color:#61aeee">basePackage</span><span style="color:#999999">(</span><span style="color:#669900">"com.kuang.swagger.controller"</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
        <span style="color:#999999">.</span><span style="color:#61aeee">paths</span><span style="color:#999999">(</span>PathSelectors<span style="color:#999999">.</span><span style="color:#61aeee">ant</span><span style="color:#999999">(</span><span style="color:#669900">"/kuang/**"</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
        <span style="color:#999999">.</span><span style="color:#61aeee">build</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>

题目:我只希望我的Swagger在生产环境中使用,在发布的时候不使用

  • 判断是不是生产环境 flag = false
  • 注入enable (flag)
<span style="color:#000000"><code class="language-java"><span style="color:#5c6370">//配置了Swagger 的Docket的bean实例</span>
<span style="color:#999999">@Bean</span>
<span style="color:#c678dd">public</span> Docket <span style="color:#61aeee">docket</span><span style="color:#999999">(</span>Environment environment<span style="color:#999999">)</span><span style="color:#999999">{</span>

    <span style="color:#5c6370">//设置要显示的Swagger环境</span>
    Profiles profiles <span style="color:#669900">=</span> Profiles<span style="color:#999999">.</span><span style="color:#61aeee">of</span><span style="color:#999999">(</span><span style="color:#669900">"dev"</span><span style="color:#999999">,</span><span style="color:#669900">"test"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#5c6370">//通过  environment.acceptsProfiles 判断是否处在自己设定的环境中</span>
    <span style="color:#c678dd">boolean</span> flag <span style="color:#669900">=</span> environment<span style="color:#999999">.</span><span style="color:#61aeee">acceptsProfiles</span><span style="color:#999999">(</span>profiles<span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span>
<span style="color:#999999">}</span>
</code></span>

狂神说java   --springboot笔记

配置API文档的分组

<span style="color:#000000"><code class="language-java"><span style="color:#999999">.</span><span style="color:#61aeee">groupName</span><span style="color:#999999">(</span><span style="color:#669900">"狂神"</span><span style="color:#999999">)</span>
</code></span>
  •  

如何配置多个分组;多个Docket实例即可
狂神说java   --springboot笔记

五、 接口注释

狂神说java   --springboot笔记
总结:

  1. 我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息;
  2. 接口文档实时更新
  3. 可以在线测试

Swagger是一个优秀的工具,几乎所有的大公司都有使用它

【注意点】在正式发布的时候,关闭Swagger!!出于安全考虑,而且节省运行的内存。

九、任务

一、异步任务

  1. 给要实现异步任务加上注解 @Async

    <span style="color:#000000"><code class="language-java"><span style="color:#999999">@Service</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> AsyncService <span style="color:#999999">{</span>
        <span style="color:#999999">@Async</span>
        <span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">hello</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
            <span style="color:#c678dd">try</span> <span style="color:#999999">{</span>
                Thread<span style="color:#999999">.</span><span style="color:#61aeee">sleep</span><span style="color:#999999">(</span><span style="color:#98c379">3000</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#999999">}</span> <span style="color:#c678dd">catch</span> <span style="color:#999999">(</span>InterruptedException e<span style="color:#999999">)</span> <span style="color:#999999">{</span>
                e<span style="color:#999999">.</span><span style="color:#61aeee">printStackTrace</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#999999">}</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">"数据正在处理..."</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>
    </code></span>
  2. 在main方法开启异步功能 @EnableAsync

    <span style="color:#000000"><code class="language-java"><span style="color:#999999">@EnableAsync</span> <span style="color:#5c6370">// 开启异步注解功能</span>
    <span style="color:#999999">@SpringBootApplication</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> Springboot09TaskApplication <span style="color:#999999">{</span>
        <span style="color:#c678dd">public</span> <span style="color:#c678dd">static</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
            SpringApplication<span style="color:#999999">.</span><span style="color:#61aeee">run</span><span style="color:#999999">(</span>Springboot09TaskApplication<span style="color:#999999">.</span><span style="color:#c678dd">class</span><span style="color:#999999">,</span> args<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>
    </code></span>

二、邮件任务

  1. 导入依赖

    <span style="color:#000000"><code class="language-xml"><span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
        <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-mail<span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
    <span style="color:#e06c75"><span style="color:#e06c75"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
    </code></span>
  2. 配置邮箱和密码

    <span style="color:#000000"><code class="language-properties">spring.mail.username=514906666@qq.com
    spring.mail.password=xxyyzzhhll
    spring.mail.host=smtp.qq.com
    # 开启加密验证
    spring.mail.properties.mail.smtp.ssl.enable=true
    </code></span>
  3. 测试发送邮件(组合邮件,带附件)

    <span style="color:#000000"><code class="language-java"><span style="color:#999999">@SpringBootTest</span>
    <span style="color:#c678dd">class</span> Springboot09TaskApplicationTests <span style="color:#999999">{</span>
    
        <span style="color:#999999">@Autowired</span>
        JavaMailSenderImpl mailSender<span style="color:#999999">;</span>
    
        <span style="color:#999999">@Test</span>
        <span style="color:#c678dd">void</span> <span style="color:#61aeee">contextLoads2</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#c678dd">throws</span> MessagingException <span style="color:#999999">{</span>
    
            <span style="color:#5c6370">//一个复杂的邮件</span>
            MimeMessage mimeMessage <span style="color:#669900">=</span> mailSender<span style="color:#999999">.</span><span style="color:#61aeee">createMimeMessage</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#5c6370">//组装</span>
            MimeMessageHelper helper <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> MimeMessageHelper<span style="color:#999999">(</span>mimeMessage<span style="color:#999999">,</span> <span style="color:#56b6c2">true</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#5c6370">//正文</span>
            helper<span style="color:#999999">.</span><span style="color:#61aeee">setSubject</span><span style="color:#999999">(</span><span style="color:#669900">"Test"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            helper<span style="color:#999999">.</span><span style="color:#61aeee">setText</span><span style="color:#999999">(</span><span style="color:#669900">"<h2 style='color:red'> 这是一封测试邮件 </h2>"</span><span style="color:#999999">,</span><span style="color:#56b6c2">true</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            <span style="color:#5c6370">//附件</span>
            helper<span style="color:#999999">.</span><span style="color:#61aeee">addAttachment</span><span style="color:#999999">(</span><span style="color:#669900">"data1"</span><span style="color:#999999">,</span><span style="color:#c678dd">new</span> File<span style="color:#999999">(</span><span style="color:#669900">"D:\\testdata\\2017-01-01.xls"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            helper<span style="color:#999999">.</span><span style="color:#61aeee">setTo</span><span style="color:#999999">(</span><span style="color:#669900">"dishifu@126.com"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
            helper<span style="color:#999999">.</span><span style="color:#61aeee">setFrom</span><span style="color:#999999">(</span><span style="color:#669900">"514906666@qq.com"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    
            mailSender<span style="color:#999999">.</span><span style="color:#61aeee">send</span><span style="color:#999999">(</span>mimeMessage<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    </code></span>

三、定时任务

  1. TaskScheduler 任务调度者
  2. TaskExecutor 任务执行者
  3. @EnableScheduling 开启定时功能的注解
  4. @Scheduled 什么时候执行
  5. Cron表达式
  1. 编写定时服务

    <span style="color:#000000"><code class="language-java"><span style="color:#999999">@Service</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> ScheduledService <span style="color:#999999">{</span>
    
        <span style="color:#5c6370">//在一个特定的时间执行这个方法</span>
    
        <span style="color:#5c6370">//cron 表达式</span>
        <span style="color:#5c6370">//秒 分 时 日 月 周几</span>
    
        <span style="color:#5c6370">/*
            30 17 17 * * ?  每天10点15分30 执行一次
            30 0/5 10,18 * * ?  每天10点和18点,每隔五分钟执行一次
        */</span>
    
        <span style="color:#999999">@Scheduled</span><span style="color:#999999">(</span>cron <span style="color:#669900">=</span> <span style="color:#669900">"30 17 17 * * ?"</span><span style="color:#999999">)</span>
        <span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">Hello</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
            System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#61aeee">println</span><span style="color:#999999">(</span><span style="color:#669900">"hello,被执行了!"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    
    <span style="color:#999999">}</span>
    </code></span>
  2. 在启动类开启定时功能

    <span style="color:#000000"><code class="language-java"><span style="color:#999999">@EnableAsync</span> <span style="color:#5c6370">// 开启异步注解功能</span>
    <span style="color:#999999">@EnableScheduling</span> <span style="color:#5c6370">//开启定时功能的注解</span>
    <span style="color:#999999">@SpringBootApplication</span>
    <span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> Springboot09TaskApplication <span style="color:#999999">{</span>
        <span style="color:#c678dd">public</span> <span style="color:#c678dd">static</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
            SpringApplication<span style="color:#999999">.</span><span style="color:#61aeee">run</span><span style="color:#999999">(</span>Springboot09TaskApplication<span style="color:#999999">.</span><span style="color:#c678dd">class</span><span style="color:#999999">,</span> args<span style="color:#999999">)</span><span style="color:#999999">;</span>
        <span style="color:#999999">}</span>
    <span style="color:#999999">}</span>
    </code></span>

十、整合Redis

单独整理

十一、分布式Dubbo + Zookeeper

zookeeper :注册中心

dubbo-admin : 监控管理后台,查我们注册了哪些服务,哪些服务被消费了

Dubbo:jar包

步骤:

前提:zookeeper服务已启动

  1. 提供者提供服务

    • 导入依赖

    • 配置注册中心的地址,以及服务发现名,和要扫描的包

      狂神说java   --springboot笔记

    • 在想要被注册的服务上面,再增加一个注解**@Service**(dubbo包下的)

  2. 消费者如何消费

    • 导入依赖

    • 配置注册中心的地址,配置自己的服务名
      狂神说java   --springboot笔记

    • 从远程注入服务 @Reference

回顾总结:三层架构   + MVC

  架构 -->  解耦

    开发框架:

                   Spring:

                        Ioc(控制反转)  + Aop(面向切面编程)[本质:动态代理]

               Ioc(控制反转)

                     以前自己是new 对象,现在交给spring ioc容器统一管理,我们需要什么直接去拿  ioc容器就像是一个对象工厂,把对象交给工厂,工程管理对象的创建以及依赖关系,我们需要的时候直接去工厂中获取   

                    [控制反转,依赖注入]   控制反转:是一种思想,原本自己控制的事情,交给ico控制  ,依赖注入是控制反转的实现方式【对象】无需自行创建或者管理依赖关系,依赖关系将被自动注入到需要他们的对象当中

                    :将对象统一管理  :降低耦合

            Aop(面向切面编程)[本质:动态代理]

                  解决非业务代码抽取的问题,底层是动态代理,在spring内实现依赖是BeanPostProcessor ,在方法前后增加非业务代码

      spring是轻量型的java的开源框架,容器

负载均衡

微服务架构存在的问题:

  • 分布式架构会遇到的四个核心问题
  • 这么多服务,客户端该如何去访问
  • 这么多服务,服务之间如何进行通信
  • 这么多服务,如何管理
  • 服务挂了,该怎么办

==》 解决方案:

​ SpringCloud,是一套生态,就是用来解决以上分布式架构的4个问题。

​ 想使用SpringCloud,必须掌握SpringBoot,因为SpringCloud是基于SpringBoot的

1. API网关,服务路由
2. HTTP,RPC框架,异步调用
3. 服务注册与发现,高可用
4. 熔断机制,服务降级</code></span>
上一篇:数据库驱动连接问题


下一篇:老技术记录-C#+SqlServer使用SqlDependency监听数据库表变化