(资料图片)
接下我们需要自定义登陆接口,然后让SpringSecurity对这个接口放行,让用户访问这个接口的时候不用登录也能访问。
在接口中我们通过AuthenticationManager的authenticate方法来进行用户认证,所以需要在SecurityConfig中配置把AuthenticationManager注入容器。
认证成功的话要生成一个jwt,放入响应中返回。并且为了让用户下回请求时能通过jwt识别出具体的是哪个用户,我们需要把用户信息存入redis,可以把用户id作为key。
package com.example.qinghuatokendemo.Controller;import com.example.qinghuatokendemo.Domain.ResponseResult;import com.example.qinghuatokendemo.Domain.User;import com.example.qinghuatokendemo.Service.LoginServcie;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;@RestControllerpublic class LoginController { @Autowired private LoginServcie loginServcie; @PostMapping("/user/login") public ResponseResult login(@RequestBody User user){ //登录 return loginServcie.login(user); }}package com.example.qinghuatokendemo.Config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.authentication.AuthenticationManager;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.config.http.SessionCreationPolicy;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.crypto.password.PasswordEncoder;@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter { //创建BCryptPasswordEncoder注入容器 @Bean public PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override protected void configure(HttpSecurity http) throws Exception { http //关闭csrf .csrf().disable() //不通过Session获取SecurityContext .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() // 对于登录接口 允许匿名访问 .antMatchers("/user/login").anonymous() // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated(); }}package com.example.qinghuatokendemo.Service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.example.qinghuatokendemo.Domain.LoginUser;import com.example.qinghuatokendemo.Domain.User;import com.example.qinghuatokendemo.Mapper.UserMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.core.userdetails.UsernameNotFoundException;import org.springframework.stereotype.Service;import java.util.Objects;@Servicepublic class UserDetailsServiceImpl implements UserDetailsService { @Autowired private UserMapper userMapper; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { //查询用户信息 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(User::getUserName,username); User user = userMapper.selectOne(queryWrapper); //如果没有查询到用户就抛出异常 if (Objects.isNull(user)){ throw new RuntimeException("用户名或者密码错误"); } //查询对应的权限信息 //把数据封装成UserDetails返回 return new LoginUser(user); }} 打开redis以后
关键词:
焦点日报:认购率动辄400%,上海的房子还是香:有购房者“意念预定”
2023-04-25登录接口实现细节分析与登录接口代码实现 今日要闻
2023-04-25丰臻评高拉特退役:中超唯一两次拿到MVP的人退役,他独一无二-环球速递
2023-04-25中辉期货能源早盘关注:短线内原油走向还未明朗
2023-04-25甘肃民族师范学院后勤处饮食服务中心召开“温馨食堂,共享共建”交流座谈会
2023-04-25人社部:一季度三项社会保险基金总收入2.04万亿_天天最新
2023-04-25每日热门:特斯拉面向其他部分非特斯拉新能源车辆试点开放充电网络
2023-04-25海口警民接力救助受伤国家二级保护动物褐翅鸦鹃-全球热闻
2023-04-25类银河城新游《心渊梦境》发售!登陆Steam等海外全平台-全球热议
2023-04-25益阳书城24小时书店:些许灯火夜,最抚凡人心
2023-04-25焦点日报:认购率动辄400%,上海的房子还是香:有购房者“意念预定”
2023-04-25登录接口实现细节分析与登录接口代码实现 今日要闻
2023-04-25丰臻评高拉特退役:中超唯一两次拿到MVP的人退役,他独一无二-环球速递
2023-04-25中辉期货能源早盘关注:短线内原油走向还未明朗
2023-04-25甘肃民族师范学院后勤处饮食服务中心召开“温馨食堂,共享共建”交流座谈会
2023-04-25人社部:一季度三项社会保险基金总收入2.04万亿_天天最新
2023-04-25每日热门:特斯拉面向其他部分非特斯拉新能源车辆试点开放充电网络
2023-04-25海口警民接力救助受伤国家二级保护动物褐翅鸦鹃-全球热闻
2023-04-25类银河城新游《心渊梦境》发售!登陆Steam等海外全平台-全球热议
2023-04-25益阳书城24小时书店:些许灯火夜,最抚凡人心
2023-04-25Copyright 2015-2022 南方超市网版权所有 备案号:粤ICP备18023326号-21 联系邮箱:855 729 8@qq.com