Spring Boot + Spring Security

一、Spring Security 是 Spring Boot 自动识别

1、只要引入了 Spring Security 依赖,Spring Boot 就会自动启用它

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

不需要写一行配置代码,Spring Security 就会生效。

Spring Boot 启动时就会:

✔ 自动创建一整套 Security Filter

✔ 自动保护所有接口

✔ 自动生成 /login 登录页(跳转到Spring Boot的默认 formLogin登录页面,这个路径并不是自己配置的路劲)

✔ 自动要求所有请求必须登录

2、自动配置默认做了什么

行为 默认值
所有接口 必须认证
登录方式 表单登录
登录地址 /login
登出地址 /logout
用户 自动生成一个
密码 启动日志打印

二、前后端分离的项目应该怎么做?

1、为什么前后端分离时 不适合

前后端分离时,正确行为是:

情况 正确返回
未登录 401 JSON
已登录 正常数据

默认 formLogin 会:

情况 实际行为
未登录 302 跳 /login
前端 拿到 HTML
Security 仍认为你没登录

➡️ 永远无法建立登录态 尤其是前端已经写了login的界面 永远也不会跳转到前端的登录界面

2、前后端路线

显式关闭 formLogin,返回 401

你必须写一个 SecurityConfig(这是关键一步):

复制代码
package com.hj.mapserver.config;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                // 前后端分离必须关闭表单登录和HTTP基本认证
                .formLogin(formLogin -> formLogin.disable())
                .httpBasic(httpBasic -> httpBasic.disable())

                // 未登录直接返回 401,不跳页面
                .exceptionHandling(exceptionHandling ->
                        exceptionHandling.authenticationEntryPoint((req, resp, e) -> {
                            resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                        })
                )

                // 接口权限配置
                .authorizeHttpRequests(authorizeHttpRequests ->
                        authorizeHttpRequests
                                .requestMatchers("/login", "/check/**", "/query/system/user").permitAll()
                                .anyRequest().authenticated()
                )

                // 前后端分离关闭CSRF
                .csrf(csrf -> csrf.disable());

        return http.build();
    }
}
相关推荐
阿俊-全栈开发7 分钟前
LikeShop 单商户SaaS商城分销模块二开:佣金计算与结算逻辑实现
java·开发语言·微服务·likeshop·likeshop开源商城·likeshop多商户
wuminyu3 小时前
C++协程实现接收端的零拷贝Buffer管理原理剖析
java·linux·c语言·jvm·c++
Zhou1411365 小时前
SpringBoot_03_Web开发
前端·spring boot·python
蜗牛互联网6 小时前
WSL Containers GA:本地AI容器的生命周期、网络与治理验收
java·网络·人工智能·后端
龙亘川6 小时前
铭记英烈守初心,数字强基促服务:亘川智城退役军人服务系统的实践与价值
java·数据库·人工智能
OxYGC6 小时前
[AI工程] Spring AI 第廿一篇:存量 REST 接口的 MCP 化改造实录——工具从哪条路径注册、参数描述怎么写、身份怎么过去
java·人工智能·spring
Zhou1411367 小时前
SpringBoot_02_自动配置原理
java·spring boot·后端
FYKJ_20107 小时前
SSM校园失物招领系统41452-计算机课程设计、毕业设计
java·vue.js·spring boot·python·mysql·typescript·spark
kv1107 小时前
Android studio国内开发有关
java·数据库·android studio
卓怡学长7 小时前
w198基于springboot始于足下健康打卡平台的设计与实现
java·spring boot·spring·intellij-idea