在以前的文章中,我们深入探讨了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