app登录实现进行中

This commit is contained in:
winLsy
2018-01-20 18:19:53 +08:00
parent 2e46cc6ab7
commit bf6ce3c0ab
19 changed files with 184 additions and 23 deletions
@@ -0,0 +1,3 @@
- oauth2请求的用户必须有一个ROLE_USER的用户权限
- 相同的用户,会得到相同的token
@@ -1,8 +1,15 @@
package com.micro.fast.security.app;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
/**
* 自动配置入口类
* @author lsy
*/
@Configuration
@ComponentScan
public class BootStarterSecurityAppApplication {
}
@@ -0,0 +1,14 @@
package com.micro.fast.security.app.config.authorization2;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
/**
* OAuth2协议标准服务商实现配置类
* @author lsy
*/
@Configuration
@EnableAuthorizationServer
public class AuthorizationProviderConfig {
}
@@ -0,0 +1,13 @@
package com.micro.fast.security.app.config.authorization2;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
/**
* OAuth2协议标准服务提供实现配置类
* @author lsy
*/
@Configuration
@EnableResourceServer
public class AuthorizationResourceConfig {
}
@@ -0,0 +1 @@
package com.micro.fast.security.app.config.authorization2;
@@ -0,0 +1,54 @@
package com.micro.fast.security.app.config.component;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.micro.fast.security.core.browser.constant.LoginType;
import com.micro.fast.security.core.master.SecurityProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* AuthenticationFailureHandler
* 身份认证失败的后续处理类
* @author lsy
*/
@Component("msAuthenticationFailureHandler")
public class MsAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
private static final Logger log = LoggerFactory.getLogger(MsAuthenticationFailureHandler.class);
/**
* object <=> json 处理对象
*/
@Autowired
private ObjectMapper objectMapper;
/**
* 安全框架配置参数对象
*/
@Autowired
private SecurityProperties securityProperties;
@Override
public void onAuthenticationFailure(
HttpServletRequest httpServletRequest
, HttpServletResponse httpServletResponse
, AuthenticationException e)
throws IOException, ServletException {
//根据配置参数判断身份认证失败后的返回类型进行响应
if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())){
log.info("failure");
httpServletResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
httpServletResponse.setContentType("application/json;charset=UTF-8");
httpServletResponse.getWriter().write(objectMapper.writeValueAsString(e));
}else{
super.onAuthenticationFailure(httpServletRequest,httpServletResponse,e);
}
}
}
@@ -0,0 +1,62 @@
package com.micro.fast.security.app.config.component;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.micro.fast.security.core.browser.constant.LoginType;
import com.micro.fast.security.core.master.SecurityProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 身份认证成功后的处理类
* @author lsy
*/
@Component("msAuthenticationSuccessHandler")
public class MsAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
private static final Logger log = LoggerFactory.getLogger(MsAuthenticationSuccessHandler.class);
/**
* 安全框架配置参数对象
*/
@Autowired
private SecurityProperties securityProperties;
/**
* object <=> json 处理对象
*/
@Autowired
private ObjectMapper objectMapper;
/**
* 登录成功后的处理方法
* @param httpServletRequest
* @param httpServletResponse
* @param authentication 用户认证的详细信息
* @throws IOException
* @throws ServletException
*/
@Override
public void onAuthenticationSuccess(
HttpServletRequest httpServletRequest
, HttpServletResponse httpServletResponse
, Authentication authentication
) throws IOException, ServletException {
//根据配置参数判断身份认证成功后的返回类型进行响应
if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())){
log.info("登录成功");
httpServletResponse.setContentType("application/json;charset=UTF-8");
httpServletResponse.getWriter().write(objectMapper.writeValueAsString(authentication));
}else{
super.onAuthenticationSuccess(httpServletRequest,httpServletResponse,authentication);
}
}
}
@@ -0,0 +1 @@
package com.micro.fast.security.app.config.component;
@@ -0,0 +1 @@
package com.micro.fast.security.app.config;
@@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.micro.fast.security.app.BootStarterSecurityAppApplication
@@ -6,4 +6,5 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class BootStarterSecurityBrowserApplication {
}
@@ -1,7 +1,5 @@
package com.micro.fast.security.browser.config;
import com.micro.fast.security.browser.session.MsExpiredSessionStrategy;
import com.micro.fast.security.browser.session.MsInvalidSessionStrategy;
import com.micro.fast.security.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import com.micro.fast.security.core.master.SecurityConstants;
import com.micro.fast.security.core.master.SecurityProperties;
@@ -13,8 +11,6 @@ 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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@@ -177,15 +173,7 @@ public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
}
/**
* 定义身份认证红密码的加密解密类的bean
*
* @return
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
/**
* 用于记住我功能
@@ -10,11 +10,13 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.stereotype.Component;
/**
*
* @author lsy
*/
@Component
public class MsInvalidSessionStrategy extends AbstractSessionStrategy implements InvalidSessionStrategy {
public MsInvalidSessionStrategy(String invalidSessionUrl) {
@@ -1,9 +1,13 @@
package com.micro.fast.security.core;
import com.micro.fast.security.core.master.SecurityProperties;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* @author lsy
@@ -12,5 +16,14 @@ import org.springframework.context.annotation.Configuration;
@ComponentScan
@EnableConfigurationProperties(SecurityProperties.class)
public class BootStarterSecurityCoreApplication {
/**
* 定义身份认证红密码的加密解密类的bean
*
* @return
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
@@ -18,7 +18,7 @@
<dependencies>
<dependency>
<groupId>com.micro.fast</groupId>
<artifactId>boot-starter-security-browser</artifactId>
<artifactId>boot-starter-security-app</artifactId>
<version>${micro.fast.version}</version>
</dependency>
<dependency>
@@ -11,7 +11,6 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
*/
@SpringBootApplication
@RestController
@EnableSwagger2
public class BootStarterSecurityDemoApplication {
public static void main(String[] args) {
@@ -1,14 +1,14 @@
spring:
session:
store-type: redis
# session:
# store-type: redis
datasource:
url: jdbc:mysql://127.0.0.1:3317/micro?useUncoide=true&chracterEncoding=utf-8&useSSL=false
username: root
password: studyj2e
driver-class-name: com.mysql.jdbc.Driver
redis:
host: fthipw.natappfree.cc
port: 6379
# redis:
# host: fthipw.natappfree.cc
# port: 6379
#security:
# basic:
# enabled: true
@@ -29,5 +29,6 @@ server:
security:
oauth2:
client:
client-id:
client-id: 授权给谁
client-secret: 授权给谁的密码
@@ -41,7 +41,7 @@ public class DynamicDataSourceConfig {
/**
* 从数据源
*/
@Autowired(required = false)
@Autowired
@Qualifier(MASTER_DATA_SOURCE)
private DataSource masterDataSource;