日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Security和自定义密码编码

發(fā)布時(shí)間:2023/12/3 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Security和自定义密码编码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在上一篇文章中,我們使用jdbc和md5密碼編碼將密碼編碼添加到了我們的spring安全配置中。

但是,在定制UserDetailsS??ervices的情況下,我們需要對安全配置進(jìn)行一些調(diào)整。
我們需要創(chuàng)建一個DaoAuthenticationProvider bean,并將其設(shè)置為AuthenticationManagerBuilder。

由于我們需要一個Custom UserDetailsS??ervice,因此我將使用Spring Security / MongoDB示例代碼庫。

我們要做的是更改我們的Spring Security配置。

package com.gkatzioura.spring.security.config;import com.gkatzioura.spring.security.service.CustomerUserDetailsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Profile; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.authentication.encoding.Md5PasswordEncoder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import javax.sql.DataSource;/*** Created by gkatzioura on 10/5/16.*/ @EnableWebSecurity @Profile("encodedcustompassword") public class PasswordCustomEncodedSecurityConfig extends WebSecurityConfigurerAdapter {@Beanpublic UserDetailsService mongoUserDetails() {return new CustomerUserDetailsService();}@Beanpublic DaoAuthenticationProvider authProvider() {DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();authProvider.setUserDetailsService(mongoUserDetails());authProvider.setPasswordEncoder(new BCryptPasswordEncoder());return authProvider;}@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.authenticationProvider(authProvider());}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/public").permitAll().anyRequest().authenticated().and().formLogin().permitAll().and().logout().permitAll();}}

在大多數(shù)情況下,這可以。 但是,我們也可能希望推出自己的PasswordEncoder,這非常簡單。

package com.gkatzioura.spring.security.encoder;import org.springframework.security.crypto.bcrypt.BCrypt; import org.springframework.security.crypto.password.PasswordEncoder;/*** Created by gkatzioura on 10/5/16.*/ public class CustomPasswordEncoder implements PasswordEncoder {@Overridepublic String encode(CharSequence rawPassword) {String hashed = BCrypt.hashpw(rawPassword.toString(), BCrypt.gensalt(12));return hashed;}@Overridepublic boolean matches(CharSequence rawPassword, String encodedPassword) {return BCrypt.checkpw(rawPassword.toString(), encodedPassword);}}

因此,我們將更改配置以使用新的PasswordEncoder

@Beanpublic DaoAuthenticationProvider authProvider() {DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();authProvider.setUserDetailsService(mongoUserDetails());authProvider.setPasswordEncoder(new CustomPasswordEncoder());return authProvider;}

下一步將是創(chuàng)建編碼后的密碼。

@Testpublic void customEncoder() {CustomPasswordEncoder customPasswordEncoder = new CustomPasswordEncoder();String encoded = customPasswordEncoder.encode("custom_pass");LOGGER.info("Custom encoded "+encoded);}

然后將具有哈希密碼的用戶添加到我們的mongodb數(shù)據(jù)庫中。

db.users.insert({"name":"John","surname":"doe","email":"john2@doe.com","password":"$2a$12$qB.L7buUPi2RJHZ9fYceQ.XdyEFxjAmiekH9AEkJvh1gLFPGEf9mW","authorities":["user","admin"]})

我們所需要做的就是更改gradle腳本上的默認(rèn)配置文件,我們一切順利。

bootRun {systemProperty "spring.profiles.active", "encodedcustompassword" }

您可以在github上找到源代碼。

翻譯自: https://www.javacodegeeks.com/2016/10/spring-security-custom-password-encoding.html

總結(jié)

以上是生活随笔為你收集整理的Spring Security和自定义密码编码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。