• spring security教程(一)--认证


    零.简介

    【1】简介

    【2】登录校验流程

    【3】原理(入门的时候先了解一下就好)

     

    一.思路分析

    二.建表

    确保你已经建立好一张用户表,并且引入springboot,mybatis,mp,slf4j等基础依赖。
    即使你有多个角色你也可以将他们的相同信息(如用户名密码登都提取到一张表中)。并根据表编写对应实体类和mapper。

     上面是我建立的我的user表,我把学生,督导,老师,辅导员的相同信息提取到一张表中。

    1. @Data
    2. @NoArgsConstructor
    3. @AllArgsConstructor
    4. @TableName("user")
    5. public class User {
    6. @TableId(type = IdType.AUTO)
    7. private Integer id;
    8. private String no;
    9. private String password;
    10. private String name;
    11. private String gender;
    12. private String college;
    13. private String userType;
    14. }
    1. @Mapper
    2. public interface UserMapper extends BaseMapper {
    3. }

    三.引入依赖

    1. <dependency>
    2. <groupId>com.alibabagroupId>
    3. <artifactId>fastjsonartifactId>
    4. <version>1.2.62version>
    5. dependency>
    6. <dependency>
    7. <groupId>junitgroupId>
    8. <artifactId>junitartifactId>
    9. <scope>testscope>
    10. dependency>
    11. 下面这个依赖不用写版本是因为这个依赖继承spring boot,而springboot中已经有版本管理了
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-data-redisartifactId>
    15. dependency>
    16. <dependency>
    17. <groupId>org.springframework.bootgroupId>
    18. <artifactId>spring-boot-starter-securityartifactId>
    19. dependency>
    20. <dependency>
    21. <groupId>io.jsonwebtokengroupId>
    22. <artifactId>jjwtartifactId>
    23. <version>0.9.1version>
    24. dependency>

    四.工具类

    【1】redis

    (1)redis使用FastJson序列化配置

    1. package com.flyingpig.util;
    2. import com.alibaba.fastjson.JSON;
    3. import com.alibaba.fastjson.serializer.SerializerFeature;
    4. import com.fasterxml.jackson.databind.JavaType;
    5. import com.fasterxml.jackson.databind.ObjectMapper;
    6. import com.fasterxml.jackson.databind.type.TypeFactory;
    7. import org.springframework.data.redis.serializer.RedisSerializer;
    8. import org.springframework.data.redis.serializer.SerializationException;
    9. import com.alibaba.fastjson.parser.ParserConfig;
    10. import org.springframework.util.Assert;
    11. import java.nio.charset.Charset;
    12. /**
    13. * Redis使用FastJson序列化
    14. *
    15. */
    16. public class FastJsonRedisSerializer implements RedisSerializer
    17. {
    18. public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    19. private Class clazz;
    20. static
    21. {
    22. ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
    23. }
    24. public FastJsonRedisSerializer(Class clazz)
    25. {
    26. super();
    27. this.clazz = clazz;
    28. }
    29. @Override
    30. public byte[] serialize(T t) throws SerializationException
    31. {
    32. if (t == null)
    33. {
    34. return new byte[0];
    35. }
    36. return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
    37. }
    38. @Override
    39. public T deserialize(byte[] bytes) throws SerializationException
    40. {
    41. if (bytes == null || bytes.length <= 0)
    42. {
    43. return null;
    44. }
    45. String str = new String(bytes, DEFAULT_CHARSET);
    46. return JSON.parseObject(str, clazz);
    47. }
    48. protected JavaType getJavaType(Class clazz)
    49. {
    50. return TypeFactory.defaultInstance().constructType(clazz);
    51. }
    52. }

    (2)redis配置类--如果没有redis配置类,不同方法中的redis数据还是不能共用

    1. package com.flyingpig.config;
    2. import com.flyingpig.util.FastJsonRedisSerializer;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.data.redis.connection.RedisConnectionFactory;
    6. import org.springframework.data.redis.core.RedisTemplate;
    7. import org.springframework.data.redis.serializer.StringRedisSerializer;
    8. @Configuration
    9. public class RedisConfig {
    10. @Bean
    11. @SuppressWarnings(value = { "unchecked", "rawtypes" })
    12. public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory)
    13. {
    14. RedisTemplate template = new RedisTemplate<>();
    15. template.setConnectionFactory(connectionFactory);
    16. FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
    17. // 使用StringRedisSerializer来序列化和反序列化redis的key值
    18. template.setKeySerializer(new StringRedisSerializer());
    19. template.setValueSerializer(serializer);
    20. // Hash的key也采用StringRedisSerializer的序列化方式
    21. template.setHashKeySerializer(new StringRedisSerializer());
    22. template.setHashValueSerializer(serializer);
    23. template.afterPropertiesSet();
    24. return template;
    25. }
    26. }

    (3)redis工具类

    1. package com.flyingpig.util;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.data.redis.core.BoundSetOperations;
    4. import org.springframework.data.redis.core.HashOperations;
    5. import org.springframework.data.redis.core.RedisTemplate;
    6. import org.springframework.data.redis.core.ValueOperations;
    7. import org.springframework.stereotype.Component;
    8. import java.util.*;
    9. import java.util.concurrent.TimeUnit;
    10. @SuppressWarnings(value = { "unchecked", "rawtypes" })
    11. @Component
    12. public class RedisCache
    13. {
    14. @Autowired
    15. public RedisTemplate redisTemplate;
    16. /**
    17. * 缓存基本的对象,Integer、String、实体类等
    18. *
    19. * @param key 缓存的键值
    20. * @param value 缓存的值
    21. */
    22. public void setCacheObject(final String key, final T value)
    23. {
    24. redisTemplate.opsForValue().set(key, value);
    25. }
    26. /**
    27. * 缓存基本的对象,Integer、String、实体类等
    28. *
    29. * @param key 缓存的键值
    30. * @param value 缓存的值
    31. * @param timeout 时间
    32. * @param timeUnit 时间颗粒度
    33. */
    34. public void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
    35. {
    36. redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    37. }
    38. /**
    39. * 设置有效时间
    40. *
    41. * @param key Redis键
    42. * @param timeout 超时时间
    43. * @return true=设置成功;false=设置失败
    44. */
    45. public boolean expire(final String key, final long timeout)
    46. {
    47. return expire(key, timeout, TimeUnit.SECONDS);
    48. }
    49. /**
    50. * 设置有效时间
    51. *
    52. * @param key Redis键
    53. * @param timeout 超时时间
    54. * @param unit 时间单位
    55. * @return true=设置成功;false=设置失败
    56. */
    57. public boolean expire(final String key, final long timeout, final TimeUnit unit)
    58. {
    59. return redisTemplate.expire(key, timeout, unit);
    60. }
    61. /**
    62. * 获得缓存的基本对象。
    63. *
    64. * @param key 缓存键值
    65. * @return 缓存键值对应的数据
    66. */
    67. public T getCacheObject(final String key)
    68. {
    69. ValueOperations operation = redisTemplate.opsForValue();
    70. return operation.get(key);
    71. }
    72. /**
    73. * 删除单个对象
    74. *
    75. * @param key
    76. */
    77. public boolean deleteObject(final String key)
    78. {
    79. return redisTemplate.delete(key);
    80. }
    81. /**
    82. * 删除集合对象
    83. *
    84. * @param collection 多个对象
    85. * @return
    86. */
    87. public long deleteObject(final Collection collection)
    88. {
    89. return redisTemplate.delete(collection);
    90. }
    91. /**
    92. * 缓存List数据
    93. *
    94. * @param key 缓存的键值
    95. * @param dataList 待缓存的List数据
    96. * @return 缓存的对象
    97. */
    98. public long setCacheList(final String key, final List dataList)
    99. {
    100. Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
    101. return count == null ? 0 : count;
    102. }
    103. /**
    104. * 获得缓存的list对象
    105. *
    106. * @param key 缓存的键值
    107. * @return 缓存键值对应的数据
    108. */
    109. public List getCacheList(final String key)
    110. {
    111. return redisTemplate.opsForList().range(key, 0, -1);
    112. }
    113. /**
    114. * 缓存Set
    115. *
    116. * @param key 缓存键值
    117. * @param dataSet 缓存的数据
    118. * @return 缓存数据的对象
    119. */
    120. public BoundSetOperations setCacheSet(final String key, final Set dataSet)
    121. {
    122. BoundSetOperations setOperation = redisTemplate.boundSetOps(key);
    123. Iterator it = dataSet.iterator();
    124. while (it.hasNext())
    125. {
    126. setOperation.add(it.next());
    127. }
    128. return setOperation;
    129. }
    130. /**
    131. * 获得缓存的set
    132. *
    133. * @param key
    134. * @return
    135. */
    136. public Set getCacheSet(final String key)
    137. {
    138. return redisTemplate.opsForSet().members(key);
    139. }
    140. /**
    141. * 缓存Map
    142. *
    143. * @param key
    144. * @param dataMap
    145. */
    146. public void setCacheMap(final String key, final Map dataMap)
    147. {
    148. if (dataMap != null) {
    149. redisTemplate.opsForHash().putAll(key, dataMap);
    150. }
    151. }
    152. /**
    153. * 获得缓存的Map
    154. *
    155. * @param key
    156. * @return
    157. */
    158. public Map getCacheMap(final String key)
    159. {
    160. return redisTemplate.opsForHash().entries(key);
    161. }
    162. /**
    163. * 往Hash中存入数据
    164. *
    165. * @param key Redis键
    166. * @param hKey Hash键
    167. * @param value 值
    168. */
    169. public void setCacheMapValue(final String key, final String hKey, final T value)
    170. {
    171. redisTemplate.opsForHash().put(key, hKey, value);
    172. }
    173. /**
    174. * 获取Hash中的数据
    175. *
    176. * @param key Redis键
    177. * @param hKey Hash键
    178. * @return Hash中的对象
    179. */
    180. public T getCacheMapValue(final String key, final String hKey)
    181. {
    182. HashOperations opsForHash = redisTemplate.opsForHash();
    183. return opsForHash.get(key, hKey);
    184. }
    185. /**
    186. * 删除Hash中的数据
    187. *
    188. * @param key
    189. * @param hkey
    190. */
    191. public void delCacheMapValue(final String key, final String hkey)
    192. {
    193. HashOperations hashOperations = redisTemplate.opsForHash();
    194. hashOperations.delete(key, hkey);
    195. }
    196. /**
    197. * 获取多个Hash中的数据
    198. *
    199. * @param key Redis键
    200. * @param hKeys Hash键集合
    201. * @return Hash对象集合
    202. */
    203. public List getMultiCacheMapValue(final String key, final Collection hKeys)
    204. {
    205. return redisTemplate.opsForHash().multiGet(key, hKeys);
    206. }
    207. /**
    208. * 获得缓存的基本对象列表
    209. *
    210. * @param pattern 字符串前缀
    211. * @return 对象列表
    212. */
    213. public Collection keys(final String pattern)
    214. {
    215. return redisTemplate.keys(pattern);
    216. }
    217. }
    218. 【2】JWT

      1. package com.flyingpig.util;
      2. import io.jsonwebtoken.Claims;
      3. import io.jsonwebtoken.JwtBuilder;
      4. import io.jsonwebtoken.Jwts;
      5. import io.jsonwebtoken.SignatureAlgorithm;
      6. import javax.crypto.SecretKey;
      7. import javax.crypto.spec.SecretKeySpec;
      8. import java.util.Base64;
      9. import java.util.Date;
      10. import java.util.Map;
      11. import java.util.UUID;
      12. public class JwtUtil {
      13. //有效期为
      14. public static final Long JWT_TTL = 60 * 60 *1000L;// 60 * 60 *1000 一个小时
      15. //设置秘钥明文
      16. public static final String JWT_KEY = "Zmx5aW5ncGln";//flyingpig
      17. public static String getUUID(){
      18. String token = UUID.randomUUID().toString().replaceAll("-", "");
      19. return token;
      20. }
      21. /**
      22. * 生成jtw
      23. * @param subject token中要存放的数据(json格式)
      24. * @return
      25. */
      26. public static String createJWT(String subject) {
      27. JwtBuilder builder = getJwtBuilder(subject, null, getUUID());// 设置过期时间
      28. return builder.compact();
      29. }
      30. /**
      31. * 生成jtw
      32. * @param subject token中要存放的数据(json格式)
      33. * @param ttlMillis token超时时间
      34. * @return
      35. */
      36. public static String createJWT(String subject, Long ttlMillis) {
      37. JwtBuilder builder = getJwtBuilder(subject, ttlMillis, getUUID());// 设置过期时间
      38. return builder.compact();
      39. }
      40. private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {
      41. SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
      42. SecretKey secretKey = generalKey();
      43. long nowMillis = System.currentTimeMillis();
      44. Date now = new Date(nowMillis);
      45. if(ttlMillis==null){
      46. ttlMillis=JwtUtil.JWT_TTL;
      47. }
      48. long expMillis = nowMillis + ttlMillis;
      49. Date expDate = new Date(expMillis);
      50. return Jwts.builder()
      51. .setId(uuid) //唯一的ID
      52. .setSubject(subject) // 主题 可以是JSON数据
      53. .setIssuer("flyingpig") // 签发者
      54. .setIssuedAt(now) // 签发时间
      55. .signWith(signatureAlgorithm, secretKey) //使用HS256对称加密算法签名, 第二个参数为秘钥
      56. .setExpiration(expDate);
      57. }
      58. /**
      59. * 创建token
      60. * @param id
      61. * @param subject
      62. * @param ttlMillis
      63. * @return
      64. */
      65. public static String createJWT(String id, String subject, Long ttlMillis) {
      66. JwtBuilder builder = getJwtBuilder(subject, ttlMillis, id);// 设置过期时间
      67. return builder.compact();
      68. }
      69. public static void main(String[] args) throws Exception {
      70. String jwtKey = "flyingpig";
      71. String encodedKey = Base64.getEncoder().encodeToString(jwtKey.getBytes());
      72. System.out.println(encodedKey);
      73. }
      74. /**
      75. * 生成加密后的秘钥 secretKey
      76. * @return
      77. */
      78. public static SecretKey generalKey() {
      79. byte[] encodedKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
      80. SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
      81. return key;
      82. }
      83. //解析JWT令牌
      84. public static Claims parseJwt(String jwt) {
      85. SecretKey secretKey = generalKey();
      86. return Jwts.parser()
      87. .setSigningKey(secretKey)
      88. .parseClaimsJws(jwt)
      89. .getBody();
      90. }
      91. }

      【3】WebUtils

      1. package com.flyingpig.util;
      2. import javax.servlet.http.HttpServletResponse;
      3. import java.io.IOException;
      4. public class WebUtils
      5. {
      6. /**
      7. * 将字符串渲染到客户端,往响应当中去写入数据
      8. *
      9. * @param response 渲染对象
      10. * @param string 待渲染的字符串
      11. * @return null
      12. */
      13. public static String renderString(HttpServletResponse response, String string) {
      14. try
      15. {
      16. response.setStatus(200);
      17. response.setContentType("application/json");
      18. response.setCharacterEncoding("utf-8");
      19. response.getWriter().print(string);
      20. }
      21. catch (IOException e)
      22. {
      23. e.printStackTrace();
      24. }
      25. return null;
      26. }
      27. }

      五.核心代码实现

      1.编写UserDetailsServiceImpl实现UserDetailsService接口--通过用户名从数据库查询信息

      1. package com.flyingpig.service.serviceImpl;
      2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
      3. import com.flyingpig.dto.LoginUser;
      4. import com.flyingpig.mapper.UserMapper;
      5. import com.flyingpig.untity.User;
      6. import org.springframework.beans.factory.annotation.Autowired;
      7. import org.springframework.security.core.userdetails.UserDetails;
      8. import org.springframework.security.core.userdetails.UserDetailsService;
      9. import org.springframework.security.core.userdetails.UsernameNotFoundException;
      10. import org.springframework.stereotype.Service;
      11. import java.util.List;
      12. import java.util.Objects;
      13. @Service
      14. public class UserDetailsServiceImpl implements UserDetailsService {
      15. @Autowired
      16. private UserMapper userMapper;
      17. @Override
      18. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
      19. //查询用户信息
      20. QueryWrapper queryWrapper = new QueryWrapper<>();
      21. queryWrapper.eq("no",username);
      22. User user = userMapper.selectOne(queryWrapper);
      23. //如果没有查询到用户就抛出异常
      24. if(Objects.isNull(user)){
      25. throw new RuntimeException("用户名或者密码错误");
      26. }
      27. //TODO 根据用户查询权限信息,添加到LoginUser中
      28. //把数据封装成UserDetails返回
      29. return new LoginUser(user);
      30. }
      31. }

      2.编写LoginUser实现UserDetails接口--封装用户信息

      1. @Data
      2. @NoArgsConstructor
      3. @AllArgsConstructor
      4. public class LoginUser implements UserDetails {
      5. private User user;
      6. //获取用户权限
      7. @Override
      8. public Collectionextends GrantedAuthority> getAuthorities() {
      9. return null;
      10. }
      11. //判断用户名和密码是否没过期
      12. @Override
      13. public boolean isAccountNonExpired() {
      14. return true;
      15. }
      16. //返回用户名
      17. @Override
      18. public String getUsername(){
      19. return user.getNo();
      20. }
      21. //返回密码
      22. @Override
      23. public String getPassword(){
      24. return user.getPassword();
      25. }
      26. @Override
      27. public boolean isAccountNonLocked() {
      28. return true;
      29. }
      30. @Override
      31. public boolean isCredentialsNonExpired() {
      32. return true;
      33. }
      34. @Override
      35. public boolean isEnabled() {
      36. return true;
      37. }
      38. }

      3.编写登录和登出接口LoginController

      1. @RestController
      2. @CrossOrigin(origins = "*", allowedHeaders = "*")
      3. @RequestMapping("/user")
      4. public class LoginController {
      5. @Autowired
      6. private LoginServcie loginServcie;
      7. @PostMapping("/login")
      8. public Result login(@RequestBody User user) {
      9. return loginServcie.login(user);
      10. }
      11. @RequestMapping("/logout")
      12. public Result logout(){
      13. return loginServcie.logout();
      14. }
      15. }

      4.编写SecurityConfig继承WebSecurityConfigurerAdapter接口--确定密码加密方式并让spring security对登录接口允许匿名访问

      里面有各段代码的详细解释

      1. @Configuration
      2. public class SecurityConfig extends WebSecurityConfigurerAdapter {
      3. @Bean
      4. public PasswordEncoder passwordEncoder(){
      5. return new BCryptPasswordEncoder();
      6. }
      7. @Override
      8. protected void configure(HttpSecurity http) throws Exception {
      9. http
      10. //关闭csrf
      11. .csrf().disable()
      12. //不会创建HttpSession并且不通过Session获取SecurityContext对象,因为前后端分离基本session就已经灭有用了
      13. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
      14. .and()
      15. .authorizeRequests()//对请求认证规则进行相应配置
      16. //1.对于登录,修改密码和注册接口放行(antMatchers)
      17. // 并且在后面加上anonymous方法允许匿名访问而不允许在已登录状态访问
      18. //简单说加上anonymous在没有token的情况下可以访问,携带token反而不能访问,一般用于登录注册接口之类的
      19. //与之相对应的是加上permiAll(),加上后这个接口有无token(有无身份)都可以访问,一般用于静态资源的放行
      20. .antMatchers("/user/login").anonymous()
      21. //2.除上面外的所有请求任意的用户认证之后可以访问
      22. //后面会进行精细化授权
      23. .anyRequest().authenticated();
      24. }
      25. @Bean
      26. @Override
      27. public AuthenticationManager authenticationManagerBean() throws Exception {
      28. return super.authenticationManagerBean();
      29. }
      30. }

      5.编写LoginService及其impl

      1. public interface LoginService {
      2. Result login(User user);
      3. Result logout();
      4. }
      1. @Service
      2. public class LoginServiceImpl implements LoginService {
      3. @Autowired
      4. private AuthenticationManager authenticationManager;
      5. @Autowired
      6. private RedisCache redisCache;
      7. @Override
      8. public Result login(User user) {
      9. //AuthenticationManager authenticate进行用户认证
      10. UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getNo(),user.getPassword());
      11. Authentication authenticate = authenticationManager.authenticate(authenticationToken);
      12. //如果认证没通过,给出对应的提示
      13. if(Objects.isNull(authenticate)){
      14. throw new RuntimeException("登录失败");
      15. }
      16. //如果认证通过了,使用userid生成一个jwt jwt存入ResponseResult返回
      17. LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
      18. String userid = loginUser.getUser().getId().toString();
      19. String jwt = JwtUtil.createJWT(userid);
      20. Map map = new HashMap<>();
      21. map.put("token",jwt);
      22. //把完整的用户信息存入redis userid作为key
      23. redisCache.setCacheObject("login:"+userid,loginUser);
      24. return Result.success(map);
      25. }
      26. @Override
      27. public Result logout() {
      28. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
      29. LoginUser loginUser = (LoginUser) authentication.getPrincipal();
      30. Integer userid = loginUser.getUser().getId();
      31. redisCache.deleteObject("login:"+userid);
      32. return new Result(200,"退出成功",null);
      33. }
      34. }

      6.测试接口

      下载redis,并启动redis-server,用postman发送请求,返回token

       注意请求体要写对,不然接口会403报错

      7.认证JwtAuthenticationTokenFilter继承OncePerRequestFilter--自定义认证过滤器

      这个过滤器会去获取请求头中的token,对token进行解析取出其中的userid。使用userid去redis获取对应的LoginUser对象,然后封装Authentication对象存入SecurityContextHolder。

      1. @Component
      2. public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
      3. @Autowired
      4. private RedisCache redisCache;
      5. @Override
      6. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
      7. //获取请求路径
      8. String requestPath = request.getRequestURI();
      9. //判断请求路径是否为"/user/login"
      10. if (requestPath.equals("/user/login")) {
      11. //放行"/user/login"请求
      12. filterChain.doFilter(request, response);
      13. return;
      14. }
      15. //获取token
      16. String authorization = request.getHeader("Authorization");
      17. String token = authorization.replace("Bearer ", "");
      18. if (!StringUtils.hasText(token)) {
      19. //放行
      20. filterChain.doFilter(request, response);
      21. return;
      22. }
      23. //解析token
      24. String userid;
      25. try {
      26. Claims claims = JwtUtil.parseJwt(token);
      27. userid = claims.getSubject();
      28. } catch (Exception e) {
      29. e.printStackTrace();
      30. throw new RuntimeException("token非法");
      31. }
      32. //从redis中获取用户信息
      33. String redisKey = "login:" + userid;
      34. LoginUser loginUser = redisCache.getCacheObject(redisKey);
      35. if(Objects.isNull(loginUser)){
      36. throw new RuntimeException("用户未登录");
      37. }
      38. //存入SecurityContextHolder
      39. //TODO 获取权限信息封装到Authentication中
      40. UsernamePasswordAuthenticationToken authenticationToken =
      41. new UsernamePasswordAuthenticationToken(loginUser,null,loginUser.getAuthorities());
      42. SecurityContextHolder.getContext().setAuthentication(authenticationToken);
      43. //放行
      44. filterChain.doFilter(request, response);
      45. }
      46. }

      这个token校验过滤器会自动添加到过滤器链中

      注意:我这里token传过来使用的格式是:Authorization: Bearer

      当然你也可以简单的使用:token:,只要改一下过滤器中获取token的那一两句代码就可以了。

      六.补充

      1.从Redis中获取到loginUser的信息

      在其他接口中,你可以通过以下步骤来从缓存中获取 `loginUser`:

      1. 首先,确保已经注入了RedisCache对象。可以使用@Autowired注解将RedisCache注入到其他接口的类中。

      1.    @Autowired
      2.    private RedisCache redisCache;

      2. 在需要实现loginSeriveImpl接口中,通过用户的userid构建缓存的 key 值,前面已经写好了

      1. //把完整的用户信息存入redis userid作为key
      2. redisCache.setCacheObject("login:"+userid,loginUser);

      即里面的这行代码。

      3. 在需要用到loginUser的地方使用redisCache对象从缓存中获取loginUser

      LoginUser loginUser = redisCache.getCacheObject(cacheKey);

      4. 确认获取到了 `loginUser`

      1.    if (Objects.isNull(loginUser)) {
      2.        // 缓存中没有对应的登录信息
      3.        // 处理缓存中没有登录信息的情况
      4.    } else {
      5.        // 缓存中有对应的登录信息
      6.        // 处理缓存中的登录信息
      7.    }

      在处理缓存中没有登录信息的情况下,你可以根据实际需求进行错误处理或者重新进行用户认证。

      2.解决跨域问题

      资料--什么是跨域问题?
       

      【1】在只有springboot的时候只需要在springboot中处理跨域问题即可。

      1. @Configuration
      2. public class CorsConfig {
      3. /**
      4. * 允许跨域调用的过滤器
      5. */
      6. @Bean
      7. public CorsFilter corsFilter() {
      8. CorsConfiguration config = new CorsConfiguration();
      9. //允许白名单域名进行跨域调用
      10. config.addAllowedOriginPattern("*");
      11. //允许跨越发送cookie
      12. config.setAllowCredentials(true);
      13. //放行全部原始头信息
      14. config.addAllowedHeader("*");
      15. //允许所有请求方法跨域调用
      16. config.addAllowedMethod("*");
      17. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
      18. source.registerCorsConfiguration("/**", config);
      19. return new CorsFilter(source);
      20. }
      21. }

      【2】加入springsecurity之后,除了要上面的类,还需要在前面的SecurityConfig的config方法中允许跨域:

      1. //允许跨域
      2. http.cors();

      配置类中的完整代码如下:

      1. @Configuration
      2. public class SecurityConfig extends WebSecurityConfigurerAdapter {
      3. @Bean
      4. public PasswordEncoder passwordEncoder(){
      5. return new BCryptPasswordEncoder();
      6. }
      7. @Autowired
      8. JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
      9. @Override
      10. protected void configure(HttpSecurity http) throws Exception {
      11. http
      12. //关闭csrf
      13. .csrf().disable()
      14. //不通过Session获取SecurityContext
      15. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
      16. .and()
      17. .authorizeRequests()
      18. // 对于登录接口 允许匿名访问
      19. .antMatchers("/user/login").anonymous()
      20. // 除上面外的所有请求全部需要鉴权认证
      21. .anyRequest().authenticated();
      22. //把token校验过滤器添加到过滤器链中
      23. http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
      24. //允许跨域
      25. http.cors();
      26. }
      27. @Bean
      28. @Override
      29. public AuthenticationManager authenticationManagerBean() throws Exception {
      30. return super.authenticationManagerBean();
      31. }
      32. }

    219. 相关阅读:
      科大讯飞发布讯飞星火 3.0;开源AI的现状
      使用Generator处理二叉树的中序遍历
      深度学习 Transformer架构解析
      Spring整合MyBatis导致一级缓存失效问题
      笔记_HTML+CSS精选
      Python数据分析
      深度自编码网络的集成学习ICPS入侵检测模型
      cmake简洁教程 - 第五篇
      机车整备场数字孪生 | 图扑智慧铁路
      Linux常用命令之top监测
    220. 原文地址:https://blog.csdn.net/bjjx123456/article/details/132414814