當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Security和自定义密码编码
生活随笔
收集整理的這篇文章主要介紹了
Spring Security和自定义密码编码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在上一篇文章中,我們使用jdbc和md5密碼編碼將密碼編碼添加到了我們的spring安全配置中。
但是,在定制UserDetailsS??ervices的情況下,我們需要對安全配置進行一些調整。
我們需要創建一個DaoAuthenticationProvider bean,并將其設置為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();}}在大多數情況下,這可以。 但是,我們也可能希望推出自己的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;}下一步將是創建編碼后的密碼。
@Testpublic void customEncoder() {CustomPasswordEncoder customPasswordEncoder = new CustomPasswordEncoder();String encoded = customPasswordEncoder.encode("custom_pass");LOGGER.info("Custom encoded "+encoded);}然后將具有哈希密碼的用戶添加到我們的mongodb數據庫中。
db.users.insert({"name":"John","surname":"doe","email":"john2@doe.com","password":"$2a$12$qB.L7buUPi2RJHZ9fYceQ.XdyEFxjAmiekH9AEkJvh1gLFPGEf9mW","authorities":["user","admin"]})我們所需要做的就是更改gradle腳本上的默認配置文件,我們一切順利。
bootRun {systemProperty "spring.profiles.active", "encodedcustompassword" }您可以在github上找到源代碼。
翻譯自: https://www.javacodegeeks.com/2016/10/spring-security-custom-password-encoding.html
總結
以上是生活随笔為你收集整理的Spring Security和自定义密码编码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何下载微信里的小视频微信视频如何下载到
- 下一篇: jax-rs jax-ws_JAX-RS