springboot版本升级,及解决springsecurity漏洞问题

<spring-cloud.version>Edgware.SR2</spring-cloud.version>
<spring-boot.version>1.5.10.RELEASE </spring-boot.version>



升级到2.x版本
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>

问题一:项目改造前是自定义的springboot依赖,现要求去掉原先的自定义封装依赖

刚开始我是把原先自定义的springboot依赖的父依赖直接移到最外层,大概过程如下,导致出现一堆问题。

坑一:类不存在,或包不存在

项目中出现大量的类不存在,但是明明可以用鼠标点击进去,此类问题经常是idea,什么缓存,或者是pom依赖下载不全导致,然后就陷入了这个误区,一直以为是idea的问题。

先是清理 idea缓存、执行mvn idea:idea命令等方式很多次还是无法解决。

然后分支代码提交后,同事检出也是同样的问题,项目还是无法启动,保错也是提示类不存在等。

此时我就怀疑可能不是idea的问题了。

后面就换了一种方式

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.10.RELEASE</version>
   <relativePath />
</parent>

改成

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-dependencies</artifactId>
   <version>${spring-boot.version}</version>
   <type>pom</type>
   <scope>import</scope>
</dependency>

妈的改成这样后终于正常了,不在提示类不存在,包不存在等问题了。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<groupId>com.gisquest.realestate</groupId>
	<artifactId>gisqrealestate-unityplatform-core</artifactId>
	<version>2.2.1.RELEASE</version>
	<modelVersion>4.0.0</modelVersion>
	<packaging>pom</packaging>
	<modules>
		<module>unityplatform-security</module>
        <module>unityplatform-microauth</module>
        <module>unityplatform-apigateway</module>
        <module>unityplatform-servicediscovery</module>
		<module>unityplatform-microappadmin</module>
        <module>unityplatform-apigateway-core</module>
        <module>unityplatform-apigateway-app</module>
		<module>unityplatform-microauth-app</module>
		<module>unityplatform-core-comm</module>
	</modules>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring-boot-admin.version>1.5.6</spring-boot-admin.version>
		<jjwt.version>0.7.0</jjwt.version>
		<commons-lang3.version>3.3.2</commons-lang3.version>
		<swagger2.version>2.7.0</swagger2.version>
		<apollo-client.version>1.0.0</apollo-client.version>
		<jackson.version>2.10.2</jackson.version>
		<!--micro version-->
		<micro-apigateway.version>2.2.1.RELEASE</micro-apigateway.version>
		<micro-auth.version>2.2.1.RELEASE</micro-auth.version>
        <micro-comm.version>2.2.1.RELEASE</micro-comm.version>
		<micro-apigateway.img.version>2.2.RELEASE</micro-apigateway.img.version>
		<micro-auth.img.version>2.2.RELEASE</micro-auth.img.version>
		<micro.img.version>2.2.RELEASE</micro.img.version>
		<com.hanweb.SecurityUtil.version>1.0</com.hanweb.SecurityUtil.version>
		<!--XStream -->
		<xstream.version>1.4.19</xstream.version>
		<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
		<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
		<eureka.version>2.1.0.RELEASE</eureka.version>
		<openfeign.version>2.0.0.RELEASE</openfeign.version>
		<zuul.version>2.1.2.RELEASE</zuul.version>
		<commons-lang.version>2.6</commons-lang.version>
		<commons-collections.version>3.2.2</commons-collections.version>
		<commons-beanutils.version>1.9.3</commons-beanutils.version>
	</properties>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>${spring-boot.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>

			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>

	 
</project>

坑二:idea中maven设置,最好调整成maven运行(推荐)

否则会出现稀奇古怪的问题,比如上面的之前用parent依赖方式我本地可以跑(奇了怪),但是同事拉下拉却跑不了,切换成eclipse也同样项目跑不起来。

经过一番折腾代码是跑起来了总算不提示类不存等问题。

代码兼容问题

org.springframework.data.redis.serializer.SerializationException: Cannot deserialize

org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.io.InvalidClassException: org.springframework.security.core.authority.SimpleGrantedAuthority; local class incompatible: stream classdesc serialVersionUID = 420, local class serialVersionUID = 510
    at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:84) ~[spring-data-redis-2.1.10.RELEASE.jar!/:2.1.10.RELEASE]

问题二:alibaba.fastjson.JSONObject cannot be cast to xx

java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to com.gisquest.realestate.security.validate.code.ValidateCode

改造前逻辑


    @Override
    public ValidateCode get(ServletWebRequest request, ValidateCodeType validateCodeType) {
        Object object = redisTemplate.opsForValue().get(buildKey(request, validateCodeType));
        if (object == null) {
            return null;
        }
   
        return (ValidateCode)object ;
    }

改造后



    @Override
    public ValidateCode get(ServletWebRequest request, ValidateCodeType validateCodeType) {
        Object object = redisTemplate.opsForValue().get(buildKey(request, validateCodeType));
        if (object == null) {
            return null;
        }
        String josn =JSON.toJSONString(object);
        return JSONObject.parseObject(josn,ValidateCode.class);
    }

问题解决!!!!

问题三:   升级后通过feign调用认证服务接口/oauth/check_token接口提示如下 Illegal hexadecimal character - at index 8

org.apache.commons.codec.DecoderException: Illegal hexadecimal character - at index 8
    at org.apache.commons.codec.binary.Hex.toDigit(Hex.java:286)
    at org.apache.commons.codec.binary.Hex.decodeHex(Hex.java:106)
    at com.gisquest.realestate.utils.Encodes.decodeHex(Encodes.java:41)

原因版本问题

改造前spring-security-core-4.2.4.RELEASE

在AbstractUserDetailsAuthenticationProvider在实现类中additionalAuthenticationChecks方法逻辑如下

	protected void additionalAuthenticationChecks(UserDetails userDetails,
			UsernamePasswordAuthenticationToken authentication)
			throws AuthenticationException {
		Object salt = null;

		if (this.saltSource != null) {
			salt = this.saltSource.getSalt(userDetails);
		}

		if (authentication.getCredentials() == null) {
			logger.debug("Authentication failed: no credentials provided");

			throw new BadCredentialsException(messages.getMessage(
					"AbstractUserDetailsAuthenticationProvider.badCredentials",
					"Bad credentials"));
		}

		String presentedPassword = authentication.getCredentials().toString();

		if (!passwordEncoder.isPasswordValid(userDetails.getPassword(),
				presentedPassword, salt)) {
			logger.debug("Authentication failed: password does not match stored value");

			throw new BadCredentialsException(messages.getMessage(
					"AbstractUserDetailsAuthenticationProvider.badCredentials",
					"Bad credentials"));
		}
	}

改造后

spring-security-core-5.1.6.RELEASE版本中AbstractUserDetailsAuthenticationProvider在实现类中改方法additionalAuthenticationChecks

   protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        if (authentication.getCredentials() == null) {
            this.logger.debug("Authentication failed: no credentials provided");
            throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
        } else {
            String presentedPassword = authentication.getCredentials().toString();
            if (!this.passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
                this.logger.debug("Authentication failed: password does not match stored value");
                throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
            }
        }
    }

逻辑变化了

使用了自定义的密码加密类

密码加密方式发现不一样了,因此按照原先代码逻辑获取到的前后密码其实是一样的,但是升级后GisqPasswordEncoder起了作用,因此在比较时应该时加密后的匹配

因此改成这样

问题四:feign调用token校验接口,在项目中未搜索到,

后来打断点才发现竟然后spring-security-oaruth2包内部类CheckTokenEndpoint中的接口,无语,我还以为是自己定义的呢找了半天未找打!!!!!

jar包依赖冲突问题:

方式一:选择依赖的模块,鼠标右键,选择show dependency

,如果有冲突会出现报红!!!

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数网络安全工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

img

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上网络安全知识点!真正的体系化!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

802)]

[外链图片转存中…(img-rvahjkK6-1715541621802)]

[外链图片转存中…(img-QZHZV6LD-1715541621802)]

[外链图片转存中…(img-yV9JSfJb-1715541621803)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上网络安全知识点!真正的体系化!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 16
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,Spring Security目前最新的版本是5.x系列,并没有2.7版本。如果您想使用Spring Boot 2.x版本的Spring Security进行配置,可以按照下面的步骤进行: 1.在pom.xml文件中添加Spring Security的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2.创建一个继承自WebSecurityConfigurerAdapter的配置类,并覆盖configure方法进行配置。示例代码如下: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); } } ``` 上述代码中,我们通过configure方法配置了安全规则,使得/public/**的请求不需要认证,其他请求需要认证。我们还配置了一个简单的内存认证,其中用户的用户名为user,密码为password。 3.在application.properties或application.yml文件中配置登录页面的路径。示例代码如下: ```yml spring: security: login: page: /login ``` 这样,当未认证的用户访问需要认证的资源时,系统会自动跳转到登录页面。 需要注意的是,以上只是一个简单的示例,在实际应用中,您可能需要更加复杂的安全规则,也可以使用数据库或LDAP等方式进行认证。您可以参考Spring Security的官方文档来进行更深入的学习和理解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值