Spring Security 前端密钥加密传输/后端解密处理

省略前端密钥加密处理...

import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SignInPasswordDecryptionFilter extends OncePerRequestFilter {

    private static final AntPathRequestMatcher DEFAULT_ANT_PATH_REQUEST_MATCHER = new AntPathRequestMatcher("/login", HttpMethod.POST.name());

    @Override
    protected void doFilterInternal(
            HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        if (DEFAULT_ANT_PATH_REQUEST_MATCHER.matcher(request).isMatch()) {
            filterChain.doFilter(new FormContentRequestWrapper(request), response);
        } else {
            filterChain.doFilter(request, response);
        }
    }


    private static class FormContentRequestWrapper extends HttpServletRequestWrapper {

        public FormContentRequestWrapper(HttpServletRequest request) {
            super(request);
        }

        @Override
        @Nullable
        public String getParameter(String name) {
            String queryStringValue = super.getParameter(name);

            if (UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY.equals(name)) {
                // 解密操作
                queryStringValue = "123456";
            }

            return queryStringValue;
        }

    }

}
http.addFilterBefore(new SignInPasswordDecryptionFilter(), UsernamePasswordAuthenticationFilter.class);

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
Spring Security本身并不提供DESede加密算法的实现,但你可以使用Java的标准库来实现。下面是一个示例代码,演示了如何在Spring Security中使用DESede进行加密。 首先,你需要创建一个自定义的PasswordEncoder类来实现加密解密方法。你可以使用`javax.crypto`包中的`Cipher`类来执行加密解密操作。下面是一个简单的示例: ```java import org.springframework.security.crypto.password.PasswordEncoder; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import java.security.spec.KeySpec; import java.util.Base64; public class MyPasswordEncoder implements PasswordEncoder { private static final String ENCRYPTION_ALGORITHM = "DESede"; private static final String SECRET_KEY = "your_secret_key"; // 自定义的密钥 @Override public String encode(CharSequence rawPassword) { try { Cipher cipher = getCipher(Cipher.ENCRYPT_MODE); byte[] encryptedBytes = cipher.doFinal(rawPassword.toString().getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } catch (Exception e) { throw new RuntimeException("Error occurred while encoding password", e); } } @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { try { Cipher cipher = getCipher(Cipher.DECRYPT_MODE); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encodedPassword)); String decryptedPassword = new String(decryptedBytes); return rawPassword.toString().equals(decryptedPassword); } catch (Exception e) { throw new RuntimeException("Error occurred while decoding password", e); } } private Cipher getCipher(int cipherMode) throws Exception { KeySpec keySpec = new DESedeKeySpec(SECRET_KEY.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM); SecretKey key = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); cipher.init(cipherMode, key); return cipher; } } ``` 然后在Spring Security的配置类中,将自定义的PasswordEncoder实例注入到AuthenticationManagerBuilder中: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private MyPasswordEncoder passwordEncoder; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .passwordEncoder(passwordEncoder) .withUser("user") .password("your_encrypted_password") // 加密后的密码 .roles("USER"); } // 其他配置... } ``` 这样,你就可以在Spring Security中使用自定义的DESede加密算法来加密和验证密码了。请注意,为了安全起见,你应该将密钥存储在安全的地方,并且不要直接将明文密码存储在代码中。此示例仅用于演示目的,实际应用中请根据具体需求进行安全处理
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值