update
This commit is contained in:
+11
-1
@@ -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
|
||||
|
||||
+50
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+58
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -25,7 +25,7 @@ public class MsUserDetialsService implements UserDetailsService {
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Resource(name = "userServiceImpl")
|
||||
// @Resource(name = "userServiceImpl")
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
|
||||
+10
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.micro.fast.security.core.config.param;
|
||||
|
||||
/**
|
||||
* Created by root on 12/5/17.
|
||||
*/
|
||||
public enum LoginType {
|
||||
REDIRECT,JSON;
|
||||
}
|
||||
+1
-1
@@ -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 {
|
||||
|
||||
+2
-1
@@ -15,5 +15,6 @@ spring:
|
||||
ms:
|
||||
security:
|
||||
browser:
|
||||
html:
|
||||
html: /signIn.html
|
||||
loginType: REDIRECT
|
||||
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>hello</h1>
|
||||
</body>
|
||||
</html>
|
||||
+5
@@ -6,5 +6,10 @@
|
||||
</head>
|
||||
<body>
|
||||
<h2>Demo登录页</h2>
|
||||
<form action="/user/login" method="post">
|
||||
<input name="username" type="input"/>
|
||||
<input name="password" type="password"/>
|
||||
<button type="submit">login</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user