springboot学习过程随记

1.整合shiro+jwt(若忘记需结合测试代码springboot-mybatisplus-shiro-demo看)

配置比较简单 定义一个类继承AuthorizingRealm 如下:

(1)public class AccountRealm extends AuthorizingRealm
重写里面的三个方法
supports 令牌支持
doGetAuthorizationInfo 权限获取

doGetAuthenticationInfo身份验证(重点)
其中protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
入参token即jwttoken 就在这个类中定义token校验 失败抛异常


(2)接着在@Configuration类定义@bean

DefaultWebSecurityManager securityManager
将AccountRealm传入配置(这部分代码是配置项)


(3)最后开启shiro
在需要开启shiro的方法中加上
@RequiresAuthentication即开启



2.开启跨域(cross方式)
在视图解析器中重写addCorsMappings
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
在需要跨域的方法中加上@CrossOrigin


3.自定义启动类
(1)新建一个模块 pom.xml只保留spring-boot-starter 其他的都删掉
(2)主类加注解
@Configuration
@ConditionalOnWebApplication //注解意思是web项目时开启
@EnableConfigurationProperties(HelloProperties.class)
  //HelloProperties.java中需有配置@ConfigurationProperties,这个注解让启动时HelloProperties注入 我自己理解类似于@component
(3)接着在需启动的模块中加上启动的jar包就可以了


posted @ 2020-12-14 12:12  小白小白小白小白  阅读(78)  评论(0编辑  收藏  举报