SpringBoot整合SpringSecurity-环境搭建

springboot的jar包地址:

Spring Boot Reference Guide

导入依赖:

thymeleaf:

		<dependency>
			<groupId>org.thymeleaf</groupId>
			<artifactId>thymeleaf-spring5</artifactId>
		</dependency>
		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-java8time</artifactId>
		</dependency>

 SpringSecurity:

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

 简介

地址:

Spring Boot Reference Guide

SpringSecurity 是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,只需要引入 spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理,SpringSecurity的两个主要目标是“认证”和“授权”(访问控制)
 

package com.working.study.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * WebSecurityConfigurerAdapter: 自定义Security策略。
 *
 * AuthenticationManagerBuilder: 自定义认证策略。
 *
 * @EnableWebSecurity: 开启WebSecurity模式。
 */
@EnableWebSecurity
public class UserSecurityConfig extends WebSecurityConfigurerAdapter {
    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //授权规则
        http.authorizeRequests()
                // “/”下的都可以访问
                .antMatchers("/").permitAll()
                // “/”下的只有“gj”可以访问
                .antMatchers("/gj").hasRole("gj");
        //配置没有权限默认页面
        http.formLogin();
    }
    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //数据库(jdbc)里进行数据认证
        //auth.jdbcAuthentication().dataSource(dataSource)
        //内存里进行数据认证
        auth.inMemoryAuthentication()
                //设置密码编码加密
                .passwordEncoder(new BCryptPasswordEncoder())
                .withUser("gj").password(new BCryptPasswordEncoder().encode("666666")).roles("权限1","权限2","权限3","...")
                //通过and()拼接
                .and()
                .withUser("gj1").password(new BCryptPasswordEncoder().encode("997997")).roles("权限1","权限2");

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring SecuritySpring 生态系统中非常受欢迎的安全框架,它提供了许多安全特性,如身份验证、授权、攻击防护等。Spring BootSpring 框架的一个扩展,可以帮助我们快速搭建 Spring 应用程序。在这里,我将介绍如何整合 Spring BootSpring Security。 首先,在 pom.xml 文件中添加以下依赖: ```xml <dependencies> <!-- Spring Boot Web Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Security Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies> ``` 然后,在应用程序主类上添加 @EnableWebSecurity 注解,这个注解会自动配置 Spring Security: ```java @SpringBootApplication @EnableWebSecurity public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 接下来,创建一个继承自 WebSecurityConfigurerAdapter 的配置类,覆盖默认的 Spring Security 配置: ```java @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin().loginPage("/login").permitAll() .and() .logout().permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER") .and() .withUser("admin").password("{noop}password").roles("USER", "ADMIN"); } } ``` 这个配置类定义了两个方法 configure(HttpSecurity http) 和 configure(AuthenticationManagerBuilder auth)。configure(HttpSecurity http) 方法定义了应用程序的安全规则,比如所有请求都需要身份验证,登录页面为 /login,注销页面为 /logout;configure(AuthenticationManagerBuilder auth) 方法定义了用户身份验证方式,这里我们使用了一个内存中的用户存储来进行身份验证。 最后,我们可以在控制器中添加一个 /login 的请求处理方法,用于显示登录页面: ```java @Controller public class LoginController { @GetMapping("/login") public String login() { return "login"; } } ``` 这个方法返回一个名为 login 的视图,我们可以在 resources/templates 目录下创建 login.html 页面来显示登录表单。 到此为止,Spring BootSpring Security整合已经完成了,我们可以启动应用程序并访问 http://localhost:8080/login 来测试登录页面了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值