Spring Boot 配置Spring Security实现简单的访问拦截

1 Spring Security

Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架。它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权。

它的设计是基于框架内大范围的依赖的,可以被划分为以下几块。

(1)Web/Http 安全:这是最复杂的部分。通过建立 filter 和相关的 service bean 来实现框架的认证机制。当访问受保护的 URL 时会将用户引入登录界面或者是错误提示界面。

(2)业务对象或者方法的安全:控制方法访问权限的。

(3)AuthenticationManager:处理来自于框架其他部分的认证请求。

(4)AccessDecisionManager:为 Web 或方法的安全提供访问决策。会注册一个默认的,但是我们也可以通过普通 bean 注册的方式使用自定义的 AccessDecisionManager。

(5)AuthenticationProvider:AuthenticationManager 是通过它来认证用户的。

(6)UserDetailsService:跟 AuthenticationProvider 关系密切,用来获取用户信息的。

2 添加Maven依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring Security-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

3 SecurityConfig

进行请求拦截。

package com.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // 过滤请求
                .authorizeRequests()
                //允许匿名访问
                .antMatchers("/test/help").anonymous()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();
    }
}

4 创建请求URL

TestController.java:

package com.controller;

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

@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping("/help")
    public String help(){
        return "hello,Help!";
    }
    @GetMapping("/login")
    public String login(){
        return "hello,login!";
    }
}

5 调试结果

可以请求的url,会直接显示结果。

不可以请求的url,会提示403,代表没有权限访问。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中启用Spring Security通常涉及以下几个步骤: 1. 添加依赖:首先,在你的`pom.xml`或`build.gradle`文件中添加Spring Security的相关依赖。例如,如果你使用Maven,你可以添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 配置Web:在Spring Boot应用中,确保启用了Web模块。如果使用Java配置,可以在`Application`类上添加`@EnableWebSecurity`注解: ```java @SpringBootApplication @EnableWebSecurity public class Application { // ... } ``` 3. 定义Security Configuration:创建一个实现了`WebSecurityConfigurerAdapter`的类,这将用来配置你的安全规则和认证策略。例如,你可以覆盖`configure(HttpSecurity http)`方法: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user") .password(passwordEncoder().encode("password")) .roles("USER"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/public/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/") .permitAll(); } // ...其他配置,如密码编码器、会话管理等 } ``` 这里设置了简单的内存中的用户认证,后续可能需要连接到数据库或使用JWT等其他方式。 4. 创建登录页面(可选):如果你想提供一个自定义的登录页面,可以在资源目录下创建HTML文件,并配置`formLogin()`的`loginPage`属性指向这个页面。 5. 运行应用:启动Spring Boot应用,Spring Security将自动生效,根据你的配置拦截HTTP请求并处理认证。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值