Spring Security基础使用

前言

学习Spring Security 的认证和授权。本文主要介绍在前后端分离开发的情况下的使用。

一、Spring Security 的依赖


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

二、新建控制器类

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
   

	@GetMapping("/hello")
	public Object sayHello(){
   
		return "hello world";
	}


}

三、访问请求

项目启动成功后, 用浏览器访问 http://localhost:8080/hello
正常情况下浏览器会打印出来 “hello world” 字符串。但是我们因为我们引入了 Spring Security 依赖。浏览器会自动跳转到 http://localhost:8080/login 。如下图:
在这里插入图片描述

四、登录流程

在这里插入图片描述

在这里插入图片描述
思路分析
登录
1.准备登录接口,参数是账号和密码。调用ProviderManager的方法进行验证,。如果验证通过把用户信息存入Redis,键为id。根据id生成JWT响应给前端。
2.自定义userDetailsService类的实现类.在这个类中查询数据库。
校验(即如何获取用户信息思路)
1.创建JWT解析过滤器
获取token
解析token。获取id
根据id在Redis中获取用户信息
用户信息存入在SecurityContextHolder中。

五、完善依赖和配置

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        
 		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
		<dependency>
		    <groupId>io.jsonwebtoken</groupId>
		    <artifactId>jjwt</artifactId>
		    <version>0.9.1</version>
		</dependency>

整合Redis
spring.redis.database=
spring.redis.host=
spring.redis.port=6379
spring.redis.password=

/**
Redis配置类
**/
@Configuration
public class RedisConfig {
   

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
   
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 设置key的序列化方式
        template.setKeySerializer(RedisSerializer.string());
        // 设置value的序列化方式
        template.setValueSerializer(RedisSerializer.json());
        // 设置hash的key的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        // 设置hash的value的序列化方式
        template.setHashValueSerializer(RedisSerializer.json());

        template.afterPropertiesSet();
        return template;
    }

}
/**
 * 前后端分离响应封装类
 */
@Data
public class Result {
   
    /**
     * 状态码
     */
    private int code;
    /**
     * 提示信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private Object data;

    public Result(){
   
    }

    public Result(int code, String msg,Object data){
   
        this.code=code;
        this.data=data;
        this.msg=msg;
    }
    public Result(int code, String msg){
   
        this.code=code;
        this.msg=msg;
    }
    public Result(int code, Object date){
   
        this.code=code;
        this
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值