Spring Boot 的 Security 安全控制

Spring Security 是一个强大且高度可定制的身份验证和访问控制框架,完全基于 Spring 的应用程序的标准,Spring Security 为基于 Java EE 的企业应用程序提供了一个全面的安全解决方案。

安全框架最主要包括两个操作

  • 认证(Authentication) 确认用户可以访问当前系统
  • 授权(Authorization) 确定用户在当前系统中是否能够执行某个操作,即用户所拥有的功能权限。

Spring Security 适配器

在 Spring Boot 当中配置 Spring Security 非常简单,创建一个自定义类继承 WebSecurityConfigurerAdapter,并在该类中使用 @EnableWebSecurity注解,就可以通过重写 config 方法来配置所需要的安全配置。

WebSecurityConfigurerAdapter 是 Spring Security 为 web 应用提供的一个适配器,实现了 WebSecurityConfigurer 接口,提供了两个方法用于重写开发者需要的安全配置。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
    protected void configure(HttpSecurity http) throws Exception {}
    
@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {}

}

configure(HttpSecurity http) 方法中可以通过 HttpSecurityauthorizeRequests() 方法定义哪些 URL 需要被保护,哪些不需要被保护;通过 formLogin() 方法定义当前用户登录的时候,跳转到的登录界面。

configure(AuthenticationManagerBuilder auth) 方法用于创建用户和用户的角色。

用户认证

Spring Security 是通过在 configure(AuthenticationManagerBuilder auth) 完成用户认证的。使用 AuthenticationManagerBuilderinMemoryAuthentication() 方法可以向内存中添加用户,并给用户指定权限。

	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		super.configure(auth);
//        在内存中添加用用户,并给用户指定角色权限
		auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN","DBA");
		auth.inMemoryAuthentication().withUser("yang").password("123456").roles("USER");
}

上边的代码中添加了两个用户,其中一个用户名是 yang 密码是 123456 用户权限是 USER ; 另一个用户名是 admin 密码是 admin 用户权限是两个 ADMIN DBA 。 需要注意的是 Spring Security 保存用户权限的时候,会默认使用 ROLE_ ,也就是说, USER 实际上是 ROLE_USER, ADMIN 是 ROLE_ADMIN ,DBA 实际上是 ROLE_DBA

当然,也可以查询数据库获取用户和权限。

用户授权

Spring Security 是通过 configure(HttpSecurity http) 完成用户授权。

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //// 开始请求权限配置
                .authorizeRequests()
                // 我们指定任何用户都可以访问多个URL的模式。
                // 任何用户都可以访问以"/resources/","/signup", 或者 "/about"开头的URL。
                .antMatchers("/assets/**","/").permitAll()
                // 请求匹配 /admin/** 只拥有 ROLE_ADMIN 角色的用户可以访问
                .antMatchers("/yang/**").hasRole("ADMIN")
                // 请求匹配 /user/** 拥有 ROLE_ADMIN 和 ROLE_USER 的角色用户都可以访问
                .antMatchers("/user/**").hasAnyRole("ADMIN", "USER")
                // 任何以"/db/" 开头的URL需要同时具有 "ROLE_ADMIN" 和 "ROLE_DBA"权限的用户才可以访问。
                // 和上面一样我们的 hasRole 方法也没有使用 "ROLE_" 前缀。
                // .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
                // 其余所有的请求都需要认证后才可以访问
                .anyRequest().authenticated();

        http
                .formLogin()
                // 登陆界面;默认登陆成功后的界面(不起作用);默认登陆失败的界面;表单提交地址
                .loginPage("/login.html")
                .loginProcessingUrl("/login")
                .failureUrl("/login.html?error")
                .permitAll()
                .and()
                .rememberMe()
                        .tokenValiditySeconds(1209600)
                        .key("rememberme")
                .and()
                .logout()
                        .logoutUrl("/logout")
                        .logoutSuccessUrl("/login.html")
                        .permitAll();

        http
                .formLogin().defaultSuccessUrl("/index.html");

    }
posted @ 2019-03-20 09:07  这块显卡有点冷  阅读(522)  评论(0编辑  收藏  举报