diff --git a/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/BrowserSecurityConfig.java b/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/BrowserSecurityConfig.java index 3167f21..366e740 100644 --- a/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/BrowserSecurityConfig.java +++ b/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/BrowserSecurityConfig.java @@ -8,6 +8,8 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 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; /** * 配置spring security使用表单登录 @@ -21,6 +23,13 @@ import org.springframework.security.crypto.password.PasswordEncoder; public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private SecurityProperties securityProperties; + + @Autowired + private AuthenticationSuccessHandler msAuthenticationSuccessHandler; + + @Autowired + private AuthenticationFailureHandler msAuthenticationFailureHandler; + /** * 配置校验方式 * @param http @@ -31,6 +40,8 @@ public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter { http.formLogin()//使用表单登录 .loginPage("/authentication/require")//自定义登录页面 .loginProcessingUrl("/user/login")//配置spring security登录接口的访问地址,用于在自定义的登录页面提交请求 + .successHandler(msAuthenticationSuccessHandler)//defined authenticationSuccessHandler + .failureHandler(msAuthenticationFailureHandler) .and() .authorizeRequests()//下面是权限配置 .antMatchers("/authentication/require",securityProperties.getBrowser().getHtml()).permitAll()//当访问这个页面的时候不进行权限校验,对用户自定义的登录页也不进行权限认证 @@ -39,7 +50,6 @@ public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter { .and() .csrf().disable();//暂时关闭掉跨站请求攻击防护 } - /** * 定义密码的加密解密类的bean * @return diff --git a/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/component/MsAuthenticationFailureHandler.java b/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/component/MsAuthenticationFailureHandler.java new file mode 100644 index 0000000..e1c930c --- /dev/null +++ b/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/component/MsAuthenticationFailureHandler.java @@ -0,0 +1,50 @@ +package com.micro.fast.security.browser.config.component; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.micro.fast.security.core.config.param.LoginType; +import com.micro.fast.security.core.config.param.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.AuthenticationFailureHandler; +import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; +import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; +import org.springframework.stereotype.Component; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by root on 12/5/17. + * AuthenticationFailureHandler + */ +@Component("msAuthenticationFailureHandler") +public class MsAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler { + private static final Logger log = LoggerFactory.getLogger(MsAuthenticationFailureHandler.class); + @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); + } + } +} diff --git a/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/component/MsAuthenticationSuccessHandler.java b/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/component/MsAuthenticationSuccessHandler.java new file mode 100644 index 0000000..87dad5d --- /dev/null +++ b/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/component/MsAuthenticationSuccessHandler.java @@ -0,0 +1,58 @@ +package com.micro.fast.security.browser.config.component; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.micro.fast.security.core.config.param.LoginType; +import com.micro.fast.security.core.config.param.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.AuthenticationSuccessHandler; +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; + +/** + * 身份认证成功后的处理类 + */ +@Component("msAuthenticationSuccessHandler") +public class MsAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler { + + private static final Logger log = LoggerFactory.getLogger(MsAuthenticationSuccessHandler.class); + + @Autowired + private SecurityProperties securityProperties; + /** + * sping mvc authenticationSuccess + */ + @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); + } + + } +} diff --git a/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/component/MsUserDetialsService.java b/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/component/MsUserDetialsService.java index 8aa3492..f1e367f 100644 --- a/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/component/MsUserDetialsService.java +++ b/boot-starter/boot-starter-security/boot-starter-security-browser/src/main/java/com/micro/fast/security/browser/config/component/MsUserDetialsService.java @@ -25,7 +25,7 @@ public class MsUserDetialsService implements UserDetailsService { @Autowired private PasswordEncoder passwordEncoder; - @Resource(name = "userServiceImpl") +// @Resource(name = "userServiceImpl") private UserService userService; /** diff --git a/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/BrowserProperties.java b/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/BrowserProperties.java index 49da524..039aadc 100644 --- a/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/BrowserProperties.java +++ b/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/BrowserProperties.java @@ -12,6 +12,8 @@ public class BrowserProperties { @NotBlank //配合SecurityProperties中的Valid注解进行参数的校验 private String html; + private LoginType loginType = LoginType.JSON; + public String getHtml() { return html; } @@ -19,4 +21,12 @@ public class BrowserProperties { public void setHtml(String html) { this.html = html; } + + public LoginType getLoginType() { + return loginType; + } + + public void setLoginType(LoginType loginType) { + this.loginType = loginType; + } } diff --git a/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/LoginType.java b/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/LoginType.java new file mode 100644 index 0000000..7769f7e --- /dev/null +++ b/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/LoginType.java @@ -0,0 +1,8 @@ +package com.micro.fast.security.core.config.param; + +/** + * Created by root on 12/5/17. + */ +public enum LoginType { + REDIRECT,JSON; +} diff --git a/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/SecurityProperties.java b/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/SecurityProperties.java index be9ccc7..5fca676 100644 --- a/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/SecurityProperties.java +++ b/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/config/param/SecurityProperties.java @@ -5,7 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import javax.validation.Valid; /** - * 用于从application.yml配置文件中加载以ms.security为前缀的属性 + * 用于从application.yml配置文件中加载以ms.security为前缀的属性, ms spring security 配置参数 */ @ConfigurationProperties(prefix = "ms.security") public class SecurityProperties { diff --git a/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/application.yml b/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/application.yml index 23d503a..504dbd2 100644 --- a/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/application.yml +++ b/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/application.yml @@ -15,5 +15,6 @@ spring: ms: security: browser: - html: + html: /signIn.html + loginType: REDIRECT diff --git a/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/static/hello.html b/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/static/hello.html new file mode 100644 index 0000000..8acd453 --- /dev/null +++ b/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/static/hello.html @@ -0,0 +1,10 @@ + + + + + Title + + +

hello

+ + \ No newline at end of file diff --git a/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/static/signIn.html b/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/static/signIn.html index 2ca6ad6..4ca715a 100644 --- a/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/static/signIn.html +++ b/boot-starter/boot-starter-security/boot-starter-security-demo/src/main/resources/static/signIn.html @@ -6,5 +6,10 @@

Demo登录页

+
+ + + +
\ No newline at end of file