Skip to content

Commit

Permalink
4-5
Browse files Browse the repository at this point in the history
  • Loading branch information
jojozhai committed Aug 20, 2017
1 parent a60c0eb commit 42f78b7
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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;

import com.imooc.security.core.properties.SecurityProperties;

Expand All @@ -23,6 +25,12 @@ public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private SecurityProperties securityProperties;

@Autowired
private AuthenticationSuccessHandler imoocAuthenticationSuccessHandler;

@Autowired
private AuthenticationFailureHandler imoocAuthenticationFailureHandler;

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
Expand All @@ -35,6 +43,8 @@ protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/authentication/require")
.loginProcessingUrl("/authentication/form")
.successHandler(imoocAuthenticationSuccessHandler)
.failureHandler(imoocAuthenticationFailureHandler)
// http.httpBasic()
.and()
.authorizeRequests()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
*
*/
package com.imooc.security.browser.authentication;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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 com.fasterxml.jackson.databind.ObjectMapper;
import com.imooc.security.core.properties.LoginType;
import com.imooc.security.core.properties.SecurityProperties;

/**
* @author zhailiang
*
*/
@Component("imoocAuthenctiationFailureHandler")
public class ImoocAuthenctiationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

private Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private ObjectMapper objectMapper;

@Autowired
private SecurityProperties securityProperties;


/* (non-Javadoc)
* @see org.springframework.security.web.authentication.AuthenticationFailureHandler#onAuthenticationFailure(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.AuthenticationException)
*/
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {

logger.info("登录失败");

if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(objectMapper.writeValueAsString(exception));
}else{
super.onAuthenticationFailure(request, response, exception);
}


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
*
*/
package com.imooc.security.browser.authentication;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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 com.fasterxml.jackson.databind.ObjectMapper;
import com.imooc.security.core.properties.LoginType;
import com.imooc.security.core.properties.SecurityProperties;

/**
* @author zhailiang
*
*/
@Component("imoocAuthenticationSuccessHandler")
public class ImoocAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

private Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private ObjectMapper objectMapper;

@Autowired
private SecurityProperties securityProperties;

/*
* (non-Javadoc)
*
* @see org.springframework.security.web.authentication.
* AuthenticationSuccessHandler#onAuthenticationSuccess(javax.servlet.http.
* HttpServletRequest, javax.servlet.http.HttpServletResponse,
* org.springframework.security.core.Authentication)
*/
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {

logger.info("登录成功");

if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())) {
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(objectMapper.writeValueAsString(authentication));
} else {
super.onAuthenticationSuccess(request, response, authentication);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
public class BrowserProperties {

private String loginPage = "/imooc-signIn.html";

private LoginType loginType = LoginType.JSON;

public String getLoginPage() {
return loginPage;
Expand All @@ -18,5 +20,13 @@ public String getLoginPage() {
public void setLoginPage(String loginPage) {
this.loginPage = loginPage;
}

public LoginType getLoginType() {
return loginType;
}

public void setLoginType(LoginType loginType) {
this.loginType = loginType;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
*
*/
package com.imooc.security.core.properties;

/**
* @author zhailiang
*
*/
public enum LoginType {

REDIRECT,

JSON

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ spring.session.store-type = none

server.port = 8060

#imooc.security.browser.loginPage = /demo-signIn.html
#imooc.security.browser.loginPage = /demo-signIn.html
imooc.security.browser.loginType = REDIRECT
10 changes: 10 additions & 0 deletions imooc-security-demo/src/main/resources/resources/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
index
</body>
</html>

0 comments on commit 42f78b7

Please sign in to comment.