SpringBoot 2.x - SpringSecurity(内存管理版)

Spring中使用的主要有两个安全框架——Shiro与SpringSecurity,本次学习的是SpringSecurity。
首先还是查看官方文档来学习。我们使用的WEB模版是Thymeleaf。

两个概念:

  • 认证(Authentication):建立声明主体的过程。一般也就是指用户登录,表示让系统知道你的存在
  • 授权(Authorization):确定一个主体是否允许在你的应用程序里执行一个运动的过程,也就是赋予你用户能干什么。

1.引入Maven

由于我们本次采用的是Thymeleaf+SpringSecurity。所以我们需要引入thymeleaf-extras-springsecurity,相关使用方法可以查看Github。

 
            org.thymeleaf.extras
            thymeleaf-extras-springsecurity4
        

2.配置类

本次采用是在内存中记录用户,大多数要点已经写在注释中,但是有一点值得注意的是,从Spring Security 5.0开始必须要设置加密方式,SpringSecurity提供了以下加密方式。当然最重要的一点,在配置类中不要忘记添加@EnableWebSecurity来开启Security功能。

SpringBoot 2.x - SpringSecurity(内存管理版)_第1张图片
Security提供的加密方式

/**
 * @author BaoZhou
 * @date 2018/6/21
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        /*匹配所有路径的*/
        http.authorizeRequests().antMatchers("/").permitAll()
                /*level1路径下需要VIP1身份才能访问*/
                .antMatchers("/level1/**").hasRole("VIP1")
                /*level1路径下需要VIP2身份才能访问*/
                .antMatchers("/level2/**").hasRole("VIP2")
                /*level1路径下需要VIP3身份才能访问*/
                .antMatchers("/level3/**").hasRole("VIP3")
                .and()
                /*
                1.formLogin系统会自动配置/login页面用于登录
                2.假如登录失败会重定向到login/error/
                 */
                .formLogin()
                /*
                定制自己的登录界面
                默认username字段提交用户名,可以通过usernameParameter自定义
                默认password字段提交密码,可以用过passwordParameter自定义
                定制了登录页面后
                登录页面地址的POST请求就是登录请求,可以用过loginProcessingUrl自定义
                */
                .loginPage("/userlogin")
                .and()
                /*开启注销功能,访问/logout并清空session*/
                .logout()
                /*注销成功去首页*/
                .logoutSuccessUrl("/")
                .and()
                .exceptionHandling()
                /*设置403错误跳转页面*/
                .accessDeniedHandler(new CustomAccessDeniedHandler())
                .and()
                /*开启记住我功能,登录会添加Cookie,点击注销会删除Cookie*/
                .rememberMe()
                /*设置记住我参数*/
                .rememberMeParameter("remember");
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth
                /*从内存中读取认证*/
                .inMemoryAuthentication()
                /*Spring Security 5.0开始必须要设置加密方式*/
                .passwordEncoder(new BCryptPasswordEncoder())
                .withUser("user1").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1")
                .and()
                .withUser("user2").password(new BCryptPasswordEncoder().encode("123")).roles("VIP2")
                .and()
                .withUser("user3").password(new BCryptPasswordEncoder().encode("123")).roles("VIP3");
    }
}

3.Web页

主要就是利用sec提供的方法。

  • sec:authorize="isAuthenticated()":是否授权成功
  • sec:authentication="principal.authorities":获取用户身份
  • sec:authentication="name":获取用户名字
  • sec:authorize="hasRole()":判断当前身份

数据页面






    
    登录界面


,你好,你是

游客你好请登录

VIP专区

自定义登录界面UserLogin




    
    用户登录


用户安全登录系统


用户名:
密码:
记住我

4.Demo GitHub地址

https://github.com/BzCoder/security

你可能感兴趣的:(SpringBoot 2.x - SpringSecurity(内存管理版))