Spring securty<一> 简介入门案例

Spring securty<一> 简介入门案例


本地项目的基础环境

环境版本
jdk1.8.0_201
maven3.6.0
Spring-boot2.3.3.RELEASE

1、简介

spring security是一个提供身份验证、授权和防止常见攻击的框架,它对命令式和反应式应用程序都有一流的支持,是保护基于Spring的应用程序的事实上的标准。

详细可以参看《spring security官网》

2、特性

2.1、身份验证

springsecurity作为身份验证,身份验证是验证试图访问特定资源的用户的身份的方法。对用户进行身份验证的常见方法是要求用户输入用户名和密码。一旦执行身份验证,我们就知道身份并可以执行授权。

2.2、资源保护,防止跨站点请求伪造(CSRF)

springsecurity提供了针对常见漏洞利用的保护。只要可能,默认情况下都会启用保护。

3、入门案例演示

新建项目badger-spring-securty-1

3.1、pom文件

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.badger</groupId>
    <artifactId>badger-spring-securty-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>badger-spring-securty-1</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.2、springboot 主启动类以及演示的controller接口

@SpringBootApplication
@RestController
public class SecurityApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SecurityApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello() {
        return "hello word ";
    }

}

3.3、启动主启动类测试

访问http://localhost:8080/hello会跳转到security默认的登录页面

账号为:user

随机密码输出在控制台上

默认登录后,就可以看到,请求已经跳转到http://localhost:8080/hello,页面也输出了hello word

3.4、自定义用户密码

启动的时候,每次都是随机的密码,也不好登录;自定义账号密码的方式,也有两种

3.4.1、yaml配置文件形式,新增application.yml配置文件
server:
  port: 8080
spring:
  security:
    user:
      name: admin
      password: 123456

配置文件,匹配的配置类为org.springframework.boot.autoconfigure.security.SecurityProperties ,部分属性信息如下:

@ConfigurationProperties(prefix = "spring.security")
public class SecurityProperties {
  private final Filter filter = new Filter();

	private User user = new User();
	
  public static class User {
		private String name = "user";

		private String password = UUID.randomUUID().toString();

		private List<String> roles = new ArrayList<>();

		private boolean passwordGenerated = true;
  }
}

上面所说的,也可以看到,默认用户就是user,默认密码是UUID;通过yaml配置文件,我们把账号密码分别改成了admin123456;再次启动,用配置好的账号密码,登录一次,就可以了;

3.4.2、使用配置类的形式
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("test").password(passwordEncoder().encode("123456"))
                .authorities("admin");
    }
}

设置一个test的账号,密码为123456,使用new BCryptPasswordEncoder()加密;再次启动然后登录,也是成功了;

写了配置类后,上述的yaml中的admin账号,就会失效了~

3.4.3 其他的形式

上面的代码auth.inMemoryAuthentication()看名称,也可以看得出来,基于内存的身份验证;

源码里,还有一些其他的验证方式,org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder.class

贴了验证的部分代码

	public InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> inMemoryAuthentication()
			throws Exception {
		return apply(new InMemoryUserDetailsManagerConfigurer<>());
	}
		public JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> jdbcAuthentication()
			throws Exception {
		return apply(new JdbcUserDetailsManagerConfigurer<>());
	}
	public LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthentication()
			throws Exception {
		return apply(new LdapAuthenticationProviderConfigurer<>());
	}

其实可以看到,有基于内存的,基于jdbc的,基于Ldap的;

当然这些,其实并不能满足实际的项目需求,来完成实际复杂的权限系统业务,后面整个的权限验证,其实都是基于自定义的验证。

详细的代码,可以查看《码云–badger-spring-security-1》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葵花下的獾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值