Spring securty<二> 配置项详解

Spring securty<二> 配置项详解


本地项目的基础环境

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

1、案例项目准备

项目的整个复制的上一次的案例,换了个项目名称badger-spring-security-2

因为项目中,按照现有的技术栈来讲,已经是前后端分离好多年了,案例写法等,都是按照前后端的模式去写,加上,我自己,也是后台开发,也好多年不写前端的代码了,前端的代码,还停留在jquery的年代,就不再去写未分离的代码了,大家见谅;

《Spring securty<一> 简介入门案例》

1.1、上节中,也说到了,在配置类里,写了具体的用户配置后,yaml文件里的配置,也会失效了~这个案例里,yaml的响应的配置也去掉了

server:
  port: 8080

1.2、上节中的配置类WebSecurityConfig,新增一个重写方法protected void configure(HttpSecurity http)

@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");
    }
	// 新增的配置,重写父级的默认的方法
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    }
}

1.3、基础案例测试

代码跟上节中,基础一致,先不在测试;

2、配置项详解

Security框架 ,整个认证、鉴权过程中,全部的配置项,比较复杂,也比较多,很多的功能,在实际应用过程中(国内使用),也需要特定的去定制化开发;

2.1、认证模块

主要说的,也是登录模块,也就是认证这块的东西。例如:

1、手机号+短信登录;

2、手机号+短信+图片验证码;

3、帐号+密码+图片验证码;

4、注册登录,默认注册页面,注册帐号后,默认登录;

5、oauth2协议的登录,QQ登录、微信登录、微博、百度、支付宝以及自建系统,三方系统的登录等;

6、扫码登录;

7、政府系统,国家规定的部分系统,CACF验签登录(类似以前银行的物理U盾登录);

8、其他的等等……

在这么多认证情况下,我们需要的是,理解整个Security的认证流程,然后根据流程,扩展式开发,定制化开发,来满足实际应用场景中,日益复杂的认证情况;

2.2、鉴权模块

就比较简单点,用户认证(登录)通过之后,拿到当前用户的信息,走权限模型 RBAC模型(Role-Based Access Control),就可以了;

2.3、Security配置

关于Security配置,上述其实也有一定的说明,spring Security的配置项非常的多,也非常的杂乱,我们更多的是需要定制化开发,来满足实际业务需要,我的整个配置介绍中,也不会每个配置项都会说到,按照第一篇博文说的,按照目前前后端分离的模式,做权限设计配置,针对业务上需要的配置,做配置项解释说明;

2.4、http.formLogin()表单登录操作详细

可以看到,整个的表单登录的配置项,也非常的多,按照实际的登录情况,分类说明实际的用法;

        formLogin.loginPage(null);//自定义登录页的路由(静态页面,或者mvc视图式的页面地址)
        formLogin.loginProcessingUrl(null);//登录接口的路由地址:必须为post请求的接口地址
        formLogin.isCustomLoginPage();// 是否为自定义登录页,true为自定义的登录页;
        formLogin.passwordParameter(null);// 登录的表单的 密码的字段名称
        formLogin.usernameParameter(null);//登录的表单的 用户名的字段名称

        formLogin.successForwardUrl(null);// 登录成功跳转的url
        formLogin.successHandler(null);// 登录成功的拦截器
        formLogin.defaultSuccessUrl(null);// 默认的成功的url

        formLogin.failureForwardUrl(null);// 失败跳转的url
        formLogin.failureHandler(null);// 失败的拦截器
        formLogin.failureUrl(null);// 失败的url

        formLogin.permitAll();// 放开不拦截,默认为true
        formLogin.permitAll(false);
		formLogin.disable();// 关闭表单的登录(其实只是拦截器链路上的一个拦截器)
	    //其他配置项
		formLogin.init(http);// 初始化方法
        formLogin.authenticationDetailsSource(null);
        formLogin.addObjectPostProcessor(null);
        formLogin.withObjectPostProcessor(null);
        HttpSecurity and = formLogin.and();
        formLogin.setBuilder(and);
        formLogin.configure(http);

需要注意的是:表单请求成功处理器、失败处理器;与loginPage冲突,配置后,loginPage不生效

FormLoginConfigurer<HttpSecurity> formLogin = http.formLogin();构造方法的源码实例

	public FormLoginConfigurer<HttpSecurity> formLogin() throws Exception {
		return getOrApply(new FormLoginConfigurer<>());
	}
	// new FormLoginConfigurer<>()
	/**
	 * Creates a new instance
	 * @see HttpSecurity#formLogin()
	 */
	public FormLoginConfigurer() {
		super(new UsernamePasswordAuthenticationFilter(), null);//父级构造,创建一个拦截器
		usernameParameter("username"); //默认的usernameParameter 是 username
		passwordParameter("password");//默认的passwordParameter 是 password
	}
	//拦截器构造方法
	public UsernamePasswordAuthenticationFilter() {
		super(new AntPathRequestMatcher("/login", "POST"));//默认的拦截的url为 /login  请求方式为post
	}

实际项目中,真实配置如下:

        // 1、表单操作
        FormLoginConfigurer<HttpSecurity> formLogin = http.formLogin();
        // 表单请求成功处理器、失败处理器;与loginPage冲突,配置后,loginPage不生效
        formLogin.successHandler(successHandler).failureHandler(failureHandler);
        // 表单提交的post请求地址,用户参数名称
        formLogin.loginProcessingUrl("/auth/login");

2.5、http.addFilter拦截器相关配置

        http.addFilter(null);// 增加一个拦截器
        http.addFilterAfter(null, null);//增加一个拦截器 在xxx拦截器之后
        http.addFilterAt(null, null);//增加一个拦截器 在xxx拦截器之前

Spring Security框架,是基于拦截器链来做认证和鉴权的, 所以拦截器也是非常重要的,当然要使一个拦截器能够在整个认证鉴权过程中,生效,配置的话,也是比较简单的,暂时不详细展开;

2.6、http.anonymous()匿名用户

http.anonymous();

在系统中,有部分资源(图片、文件、接口、css……)都有可能不需要登录,不需要权限,就能直接访问到;在Spring Security框架中,每个用户都是要有权限的,所以就有了这个匿名登录、匿名用户的概念,在配置项中,可以给匿名的用户,分配固定的角色(资源)等信息,也可以不分配,使用默认配置,用其他的配置项来分配,或者过滤掉这些资源的权限。

实际环境中,可以不配置,使用默认配置;

2.7、http.cors()跨域相关

        http.cors();
        http.cors(null);

跨域就不解释,默认配置就可以了,一般跨域操作,都是另外单独的配置;

2.8、http.csrf()CSRF攻击

        http.csrf();
        http.csrf(null);

这个也就不解释了,一般要处理,也是单独处理,前后端分离项目,这个选项关闭操作

 http.csrf().disable();//关闭csrf

2.9、http.httpBasic网页登录操作

        http.httpBasic();
        http.httpBasic(null);

目前前后端分离了,页面的跳转之类的操作,都是由前端控制了,这个关闭操作;

  http.httpBasic().disable();// 关闭 默认网页登录

2.10、http.logout()登出操作

        http.logout();
        http.logout(null);

登出操作,在前后端分离设计过程中,按照实际情况来,一般采用JWT传输,都是带有超时限制的,所以说登出操作,可以配置,也可以关闭,毕竟会默认超时;

线上配置,如果要有,可以配置一个登出处理器,也可以等JWT超时

http.logout().addLogoutHandler(null);//配置登出处理器
http.logout().disable();// 关闭登出操作;等jwt超时

2.11、oauth2协议配置

        http.oauth2Client();
        http.oauth2Client(null);
        http.oauth2Login();
        http.oauth2Login(null);
        http.oauth2ResourceServer();
        http.oauth2ResourceServer(null);

目前,整个spring Security配置,都是登录、鉴权操作,暂时不讲关于oauth2协议配置,看我时间,后续有时间,会单独开几篇博文,来说oauth2协议配置的事。

2.12、http.rememberMe()记住我功能

        http.rememberMe();
        http.rememberMe(null);

前后端分离,这个功能没有用,有另外的方式实现,默认不配置

2.13、http.sessionManagement()session会话管理器

       http.sessionManagement();
        http.sessionManagement(null);

前端后分离,分布式服务,会话存储,需要单独的服务器存储的,目前,会话的token信息,也是通过,header传输,这个功能禁止掉;

        http.sessionManagement().disable();// 关闭session 管理器

2.14、http.userDetailsService http.authenticationProvider 认证相关的处理器

        http.userDetailsService(null);//帐号密码认证
        http.authenticationProvider(null);//认证处理

这两个认证相关的配置,就是比较重要的,后面的实际代码中,会单独的说明,整个认证登录的过程中,也是围绕着这两个配置来实现的,也是应用中,主要需要写代码的地方;

2.15、http.authorizeRequests()鉴权管理

        http.authorizeRequests();
        http.authorizeRequests(null);

这个小节之前的,所有的,都是说的登录相关的,鉴权相关的,就说的这一节;所以说,整个安全框架,比较复杂的,其实也登录认证这块的;鉴权配置,相对来说,都是已经简单的;

举例配置:

 http.authorizeRequests().antMatchers("/**/*.js", "/**/*.css").permitAll().anyRequest().hasRole("admin");

.antMatchers("/**/*.js", "/**/*.css").permitAll():过滤掉

.anyRequest().hasRole("admin"):其余的,都需要admin角色,才能访问;

后面会有案例详细说明;

2.16、http.exceptionHandling()全局异常配置

        http.exceptionHandling();
        http.exceptionHandling(null);

认证、鉴权过程中,会产生一些异常信息,那么这些异常信息,直接返回给用户,肯定是不友好的,并且返回的信息格式,也不是项目规定的统一格式,那么这个全局异常信息,就需要单独的配置了;

     http.exceptionHandling().accessDeniedHandler(accessDeniedHandler)
                .authenticationEntryPoint(authenticationEntryPoint);// 全局异常配置

2.17、其他的配置

其他的配置,就暂时不在详细列出来了,上面的配置项,已经覆盖了实际项目中98%以上的操作了,剩下的配置,就是细节上的点滴配置,讲解的案例中,有实际的使用到,在小段叙说应用方式;

详细的代码,可以查看《码云》

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
I'm sorry, as an AI language model, I don't have personal experience or knowledge of specific demos related to Spring Boot Security and WeChat Mini Program integration. However, I can provide you with some general information and resources that might be helpful. Spring Boot Security is a powerful framework that provides authentication, authorization, and other security features for Spring Boot applications. WeChat Mini Program is a platform that allows developers to create mobile applications that run within the WeChat app. Integrating these two technologies can provide a secure and seamless experience for WeChat Mini Program users. To get started with Spring Boot Security and WeChat Mini Program integration, you can refer to the following resources: 1. Spring Boot Security documentation: https://docs.spring.io/spring-security/site/docs/5.5.x/reference/html5/ 2. WeChat Mini Program documentation: https://developers.weixin.qq.com/miniprogram/en/ 3. Spring Boot Security and WeChat Mini Program integration tutorial: https://www.baeldung.com/spring-security-wechat-mini-program This tutorial provides step-by-step instructions on how to integrate Spring Boot Security with WeChat Mini Program. It covers topics such as setting up a WeChat Mini Program app, configuring Spring Boot Security, and implementing authentication and authorization for WeChat Mini Program users. I hope this information helps you get started with Spring Boot Security and WeChat Mini Program integration. If you have any further questions or concerns, feel free to ask!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

葵花下的獾

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

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

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

打赏作者

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

抵扣说明:

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

余额充值