Spring Boot 整合 Spring Security(初次体验)

通过本文体验 Spring Boot 整合 Spring Security 。 Spring Security 是 Spring 家族中的一个安全管理框架,但在 Spring Boot 出现之前,使用的没有 Shiro 多,因为在 SSM/SSH 项目中整合 Spring Security 比较麻烦,直到 Spring Boot 的出现。目前关于安全管理框架的整合模式一般有两种,一种是 SSM/SSH + Shiro ,另一种是 Spring Boot/Spring Cloud + Spring Security ,但并非绝对。

1 创建工程

创建 Spring Boot 项目 spring-boot-springsecurity-helloworld ,添加 Web/Spring Security 依赖,如下:

最终的依赖如下:

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2 测试

新增测试类 HelloController ,如下:

@RestController
public class HelloController {
    // 默认用户名为 user ,密码在项目启动时打印在控制台
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

启动项目之后,浏览器访问 http://127.0.0.1:8080/hello ,会跳转到登录页面(默认用户名为 user ,密码在项目启动时打印在控制台),这是因为加入 Spring Security 依赖之后,接口就被自动保护起来了。

控制台信息:Using generated security password: 3629761f-b103-4392-8318-c7a8d43ed80d

当用户从浏览器发送请求访问 /hello 接口时,服务端会返回 302 响应码,让客户端重定向到 /login 页面,用户在 /login 页面登录,登陆成功之后,就会自动跳转到 /hello 接口。

当然也可以用 HTTP 请求工具来测试(如 Postman ),将用户信息放在请求头中,这样可以避免重定向到登录页面。

通过以上两种不同的登录方式可以看出 Spring Security 支持两种不同的认证方式:

  • 通过 form 表单来认证。
  • 通过 HttpBasic 来认证。


扫码关注微信公众号 程序员35 ,获取最新技术干货,畅聊 #程序员的35,35的程序员# 。独立站点:https://cxy35.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值