有网站模板怎么做网站,市场调研报告模板ppt,滑县网站建设,教学网站开发应用指导方案spring 安全编码在以前的文章中#xff0c;我们深入探讨了Spring安全性。 我们实现了由jdbc支持的安全性#xff0c;基于自定义 jdbc查询的安全性以及从nosql数据库检索安全性的信息。 通过足够小心#xff0c;我们会发现密码为纯文本格式。 尽管这在实际环境中可以很好地用… spring 安全编码 在以前的文章中我们深入探讨了Spring安全性。 我们实现了由jdbc支持的安全性基于自定义 jdbc查询的安全性以及从nosql数据库检索安全性的信息。 通过足够小心我们会发现密码为纯文本格式。 尽管这在实际环境中可以很好地用于示例目的但密码始终会进行编码并存储在数据库中。 Spring Security以一种非常方便的方式支持密码编码。 它带有自己的预配置密码编码器但是它也使我们能够创建自定义密码编码器。 SpringPassword安全附带有一些密码编码器例如StandardPasswordEncoderMd5PasswordEncoder和流行的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// localhost8080 / secured必须在登录提示中提供用户名TestUser和密码test_pass。 最后但并非最不重要的一点是我们将必须更改gradle.build以将encodejdbcpassword设置为默认配置文件。 bootRun {systemProperty spring.profiles.active, encodedjdbcpassword
} 您可以在github上找到源代码。 翻译自: https://www.javacodegeeks.com/2016/10/spring-security-password-encoding.htmlspring 安全编码