javascript
Spring安全性和密码编码
在以前的文章中,我們深入探討了Spring安全性。 我們實現了由jdbc支持的安全性,基于自定義 jdbc查詢的安全性以及從nosql數據庫檢索安全性的信息。
通過足夠小心,我們會發現密碼為純文本格式。 盡管這在實際環境中可以很好地用于示例目的,但密碼始終會進行編碼并以編碼方式存儲在數據庫中。
Spring Security以一種非常方便的方式支持密碼編碼。 它帶有自己的預配置密碼編碼器,但是它也使我們能夠創建自定義密碼編碼器。
SpringPassword安全附帶有一些密碼編碼器,例如StandardPasswordEncoder,Md5PasswordEncoder和流行的BCryptPasswordEncoder。
package com.gkatzioura.spring.security;import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.authentication.encoding.Md5PasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.StandardPasswordEncoder;/*** Created by gkatzioura on 10/5/16.*/ public class EncoderTest {private static final Logger LOGGER = LoggerFactory.getLogger(EncoderTest.class);@Testpublic void md5Encoder() {Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();String encoded = md5PasswordEncoder.encodePassword("test_pass",null);LOGGER.info("Md5 encoded "+encoded);}@Testpublic void bcryptEncoder() {BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();String encoded = bCryptPasswordEncoder.encode("test_pass");LOGGER.info("Becrypt encoded "+encoded);}@Testpublic void standardEncoder() {StandardPasswordEncoder standardPasswordEncoder = new StandardPasswordEncoder();String encoded = standardPasswordEncoder.encode("test_pass");LOGGER.info("Standard encoded "+encoded);}}要添加密碼編碼,我們要做的就是在spring配置中設置密碼編碼器。
使用jdbc支持的spring安全配置,這非常簡單,我們只需設置我們選擇的密碼編碼器即可。 在本例中,我們將使用md5密碼編碼器。
package com.gkatzioura.spring.security.config;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; 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 javax.sql.DataSource;/*** Created by gkatzioura on 10/5/16.*/ @EnableWebSecurity @Profile("encodedjdbcpassword") public class PasswordEncodedSecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate DataSource dataSource;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new Md5PasswordEncoder()).usersByUsernameQuery("SELECT username,password,1 FROM Custom_Users_Encoded_pass where username=?").authoritiesByUsernameQuery("SELECT username,authority FROM Custom_Roles where username=?");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/public").permitAll().anyRequest().authenticated().and().formLogin().permitAll().and().logout().permitAll();}}然后,我們將使用編碼后的密碼將用戶添加到數據庫。
drop table if exists Custom_Users_Encoded_pass; create table Custom_Users_Encoded_pass(id bigint auto_increment, username varchar(255), password varchar(255)); -- real password is test_pass insert into Custom_Users_Encoded_pass(username,password) values('TestUser','4ac1b63dca561d274c6055ebf3ed97db');因此,通過嘗試訪問http:// localhost:8080 / secured,必須在登錄提示中提供用戶名TestUser和密碼test_pass。
最后但并非最不重要的一點是,我們將必須更改gradle.build,以將encodejdbcpassword設置為默認配置文件。
bootRun {systemProperty "spring.profiles.active", "encodedjdbcpassword" }您可以在github上找到源代碼。
翻譯自: https://www.javacodegeeks.com/2016/10/spring-security-password-encoding.html
總結
以上是生活随笔為你收集整理的Spring安全性和密码编码的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: ultraos win10启动盘_Ult
- 下一篇: TikTok在美正式推出电商服务 预计1
