Spring Security使用详解(基本用法 )

1,什么是 Spring Security ?

  • Spring Security 是一个相对复杂的安全管理框架,功能比 Shiro 更加强大,权限控制细粒度更高,对 OAuth 2 的支持也更友好。
  • 由于 Spring Security 源自 Spring 家族,因此可以和 Spring 框架无缝整合,特别是 Spring Boot 中提供的自动化配置方案,可以让 Spring Security 的使用更加便捷。

2,安装配置

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

3、开始测试:

首先在项目添加一个简单的 /hello 接口:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "欢迎访问 hangge.com";
    }
}

接着启动项目,直接访问 /hello 接口则会自动跳转到登录页面(这个登录页面是由 Spring Security 提供的)
Spring Security使用详解(基本用法 )

 

 

 (3)我们必须登录后才能访问 /hello 接口。默认用户名是 user,而登录密码则在每次启动项目时随机生成,我们可以在项目启动日志中找到。
Spring Security使用详解(基本用法 )

 

 

 (4)登录后则会自动跳转到之前我访问的 /hello 接口:

4,配置用户名和密码

    如果对默认的用户名和密码不满意,可以在 application.properties 中配置默认的用户名、密码和角色。这样项目启动后就不会随机生成密码了,而是使用我们配置的用户、密码,并且登录后还具有一个 admin 角色(关于角色的用法再后面的文章会相信介绍)。
spring.security.user.name=hangge
spring.security.user.password=123
spring.security.user.roles=admin

 

基于内存的用户、URL权限配置:

1,用户角色配置:

(1)我们可以通过自定义类继承 WebSecurityConfigurerAdapter,从而实现对 Spring Security 更多的自定义配置。比如下面样例我们就配置了两个用户,以及他们对应的角色(这种方式只适合用于测试、开发环境不适用于生产)

@Configuration
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
// 指定密码的加密方式
@Bean
public PasswordEncoder passwordEncoder() {
// return new BCryptPasswordEncoder();
return new PasswordEncoder() {
@Override
public String encode(CharSequence charSequence) {
return charSequence.toString();
}

@Override
public boolean matches(CharSequence charSequence, String s) {
return Objects.equals(charSequence.toString(), s);
}
};
}
// 配置用户及其对应的角色
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
        .withUser("root").password("123").roles("ADMIN","DBA")
        .and()
        .withUser("admin").password("123").roles("ADMIN","USER")
        .and()
        .withUser("hangge").password("123").roles("USER");
    }
    // 配置 URL 访问权限
    @Override
    protected  void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests() // 开启 HttpSecurity 配置
            .antMatchers("/admin/**").hasRole("ADMIN") // admin/** 模式URL必须具备ADMIN角色
            .antMatchers("/user/**").access("hasAnyRole('ADMIN','USER')") // 该模式需要ADMIN或USER角色
            .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") // 需ADMIN和DBA角色
            .anyRequest().authenticated() // 用户访问其它URL都必须认证后访问(登录后访问)
            .and().formLogin().loginProcessingUrl("/login").permitAll() // 开启表单登录并配置登录接口
            .and().csrf().disable(); // 关闭csrf
    }
 } 

(2)配置完成后,重启项目,就可以使用这两个用户进行登录了。 

  • formLogin() 方法表示开启表单登录,即我们之前看到的登录页面。
  • loginProcessingUrl() 方法配置登录接口为“/login”,即可以直接调用“/login”接口,发起一个 POST 请求进行登录,登录参数中用户名必须为 username,密码必须为 password,配置 loginProcessingUrl 接口主要是方便 Ajax 或者移动端调用登录接口。
  • permitAll() 表示和登录相关的接口都不需要认证即可访问。

三、基于数据库的用户角色配置

maven依赖:

Spring Security使用详解(基本用法 )
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--操作数据库-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- MySQL 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- Druid 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>

        <!-- region MyBatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!--模板引擎thmeleaf对HTML的支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</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>
View Code

相关文章: