Spring Boot 中集成Security

简介

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

引入依赖

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

当然,如果你在生产环境使用spring boot security的话还是建议你固定一下版本。

使用

关闭权限验证功能

学习阶段,我们想关闭这个权限验证,如何关闭呢?
网上大部分的文章都是让我们在配置文件中进行配置

security.basic.enabled=false

但是这个方法在5.x版本之后提示过时了,不能关闭权限验证

那么我们可以使用如下方法

我们在配置类中添加如下代码,放行所有请求

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
        .anyRequest().permitAll()
        .and()
        .logout().permitAll();
    }
}

简单的覆盖默认的Configure方法

在Spring Security中的WebSecurityConfigurerAdapter提供了一些便捷的默认值,以达到WEB应用程序快速启动的目的

默认配置

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .anyRequest().authenticated() //每个请求都需要对用户进行身份验证
            .and()
        .formLogin()//开启基于表单的身份验证                    
            .and()
        .httpBasic();//开启HTTP基本身份验证                     
}

这个是我们引入的starter中的默认配置,我们需要更改它来达到我们所需要的业务场景

配置自定义的登录页面

下面,我们新建SecurityConfig类并且继承WebSecurityConfigurerAdapter,在头部开启@EnableWebSecurity注解,重写如下方法

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()//授权所有用户访问formLogin验证
            .logout()
                .permitAll();
    }

    // ...
}

我们开启表单登录验证方式,并且指向/login,但是我们在这里并没有指定logout的映射关系,所以当注销后服务端将会跳转到/login?logout

在这里我应该强调一下,在.formLogin().permitAll();//授权所有用户访问formLogin验证中,我们必须要加上permitAll()来开启所有访问者的权限,否则会因为没有权限请求/login映射导致服务端无数次跳转/login映射,那么你将会在浏览器页面中看到此网页具有重定向循环的字样

但在官方文档中,明确表示

Granting access to the formLogin() URLs is not done by default since Spring Security needs to make certain assumptions about what is allowed and what is not. To be secure, it is best to ensure granting access to resources is explicit.

formLogin()默认情况下,应不授予对URL的访问权限,因为Spring Security需要对允许的内容和不允许的内容做出某些假设。为了安全起见,最好确保授予资源的访问权限是明确的。

授予访问其他资源的权限

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/resources/**").permitAll() 
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()                                    
                .permitAll();
    }

    // ...
}

依旧是configuration,我们使用antMatchers(url)来放行URL路径,如上,.antMatchers("/resources/**").permitAll()表示,任何人都可以访问以/resource开头的url,因为我们的CSS,JS,Image等等文件都存储在这里


本作品采用知识共享署名 4.0 国际许可协议进行许可。

如果可以的话,请给我钱请给我点赞赏,小小心意即可!

Last modification:November 30, 2019
If you think my article is useful to you, please feel free to appreciate