登录接口实现细节分析与登录接口代码实现-每日热门

来源:博客园 时间:2023-04-25 11:34:09


(资料图片)

登录接口实现细节分析

登陆接口   

接下我们需要自定义登陆接口,然后让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以后

测试接口

X 关闭

登录接口实现细节分析与登录接口代码实现-每日热门

登录接口实现细节分析登陆接口接下我们需要自定义登陆接口,然后让SpringSecurity对这个接口放行,让用户访

2023-04-25

环球热门:ETF观察丨一季度净利润同比增超9%,同花顺涨超6%,数字经济ETF(159658)盘中溢价

4月24日,同花顺披露一季报,一季度该公司实现营业收入6 1亿元,同比增长18 42%;净利润1 22亿元,同比增长

2023-04-25

米哈游新游未上线超百国登顶_视焦点讯

《崩坏星穹铁道》预下载开启后,在全球超过113个国家及地区appstore免费榜登顶。

2023-04-25

券商晨会精华 | 新能源持仓进入下行趋势 TMT仍存在上行空间-当前速读

在今日的券商晨会上,东北证券认为,新能源持仓进入下行趋势,TMT仍存在上行空间。

2023-04-25

国联证券发布华海清科研报,盈利能力快速提升,合同负债持续高增

每经AI快讯,国联证券04月25日发布华海清科(688120 SH,最新价:396 9元)研报称:1)产品竞争力持续提升

2023-04-25

“2023全球华语青年戏剧导演英才计划”88部作品进入复评环节

北京日报客户端|记者王润“2023全球华语青年戏剧导演英才计划”即将进入线下复评环节,后续还将进行片段试

2023-04-25

呼啦圈怎么转的技巧_呼啦圈怎么转 全球关注

1、1 双脚站立,与肩同宽,两臂在身后呈3点和9点位置,握住呼啦圈,使之与身体保持30公分远。2、吸气挺胸,

2023-04-25

手机的好处和坏处英语作文50字 手机的好处和坏处-简讯

今天来聊聊关于手机的好处和坏处英语作文50字,手机的好处和坏处的文章,现在就为大家来简单介绍下手机的好

2023-04-25

世界今日讯!定速巡航和自适应巡航的区别哪个比较实用_定速巡航实用性

1、这是一种减轻驾车者疲劳,提高驾驶舒适性的辅助装置。2、当汽车在长距离的高速公路行驶时,启动巡航控制

2023-04-25

祁隆最好听的歌曲大全视频_祁隆最好听的歌曲大全 每日短讯

1、分手的距离---女生版恋爱花语----丝小诺自爱--阿悄因为我太傻---中文版不错哦我爱你比你爱我更多---

2023-04-25

Copyright ©   2015-2023 港澳科技网版权所有  备案号:京ICP备2023022245号-31   联系邮箱:435 226 40 @qq.com