自定义springSecurity的配置类,自定义根据用户名获取密码的逻辑,自定义密码校验逻辑
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
package com.micro.fast.security.browser;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
public class BootStarterSecurityBrowserApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BootStarterSecurityBrowserApplication.class, args);
|
||||
}
|
||||
}
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
package com.micro.fast.security.browser;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BootTstarterSecurityBrowserApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BootTstarterSecurityBrowserApplication.class, args);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.micro.fast.security.browser.config;
|
||||
|
||||
import com.micro.fast.security.browser.service.MsPasswordEncode;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
/**
|
||||
* 配置spring security使用表单登录
|
||||
* spring security的核心原理就是一组过滤器链
|
||||
* 核心过滤顺序是每经过一个过滤器这个过滤器都会做一个标记UsernamePasswordAuthenticationFiler(可配置)-> BasicAuthenticationFilter(可配置) -> 最后一个过滤器是FilterSecurityInterceptor(不可配置)这个过滤器是根据配置判断这个请求能不能访问这个资源的
|
||||
*spring security的默认用户名是user
|
||||
* 过滤器是ExceptionTranslationFilter(不可配置)是在FilterSecurityInterceptor之前的过滤器负责处理FilterSecurityInterceptor
|
||||
* 抛出的异常。根据配置进行处理
|
||||
*/
|
||||
@Configuration
|
||||
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
/**
|
||||
* 配置校验方式
|
||||
* @param http
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.formLogin()//使用表单登录
|
||||
.and()
|
||||
.authorizeRequests()//下面是权限配置
|
||||
.anyRequest()//任何请求
|
||||
.authenticated();//都需要身份认证
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义密码的加密解密类
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder(){
|
||||
return new MsPasswordEncode();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.micro.fast.security.browser.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
/**
|
||||
* Created by root on 12/4/17.
|
||||
* 处理密码的校验
|
||||
*/
|
||||
@Service
|
||||
public class MsPasswordEncode implements PasswordEncoder {
|
||||
private static final Logger log = LoggerFactory.getLogger(PasswordEncoder.class);
|
||||
/**
|
||||
* 在注册用户的时候进行密码的加密,这个方法是自己调的spring security不会自己调用,需要自己在合适的地方调用
|
||||
* @param charSequence
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String encode(CharSequence charSequence) {
|
||||
log.info("encode{}",charSequence.toString());
|
||||
return charSequence.toString()+"1";
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义自己的密码校验逻辑,这个方法spring security会在登录的时候自己调用
|
||||
* @param charSequence 用户输入的密码
|
||||
* @param s 从根据用户名获取密码的逻辑中返回的密码
|
||||
*/
|
||||
@Override
|
||||
public boolean matches(CharSequence charSequence, String s) {
|
||||
if (encode(charSequence).equals(s)){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.micro.fast.security.browser.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
/**
|
||||
* Created by root on 12/4/17.
|
||||
*
|
||||
*/
|
||||
@Service
|
||||
public class MsUserDetialsService implements UserDetailsService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MsUserDetialsService.class);
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
/**
|
||||
* 处理用户的密码获取信息
|
||||
* @param username UserDetails中包含用户的详细信息,在user中返回信息来进行详细的设定比如用户是否被锁定
|
||||
* @return
|
||||
* @throws UsernameNotFoundException
|
||||
*/
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
log.info("根据username:{}获取密码",username);
|
||||
//从数据库中去查找用户的密码 1112 是从数据库中查询出来的用户的密码
|
||||
return new User(username,passwordEncoder.encode("1112"), AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.micro.fast.security.browser.BootStarterSecurityBrowserApplication
|
||||
+1
-1
@@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class BootTstarterSecurityBrowserApplicationTests {
|
||||
public class BootStarterSecurityBrowserApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
+3
-3
@@ -9,7 +9,7 @@ spring:
|
||||
redis:
|
||||
host: 172.17.0.1
|
||||
port: 6379
|
||||
security:
|
||||
basic:
|
||||
enabled: false
|
||||
#security:
|
||||
# basic:
|
||||
# enabled: true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user