forked from jojozhai/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ain/java/com/imooc/security/browser/authentication/ImoocAuthenctiationFailureHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
...ain/java/com/imooc/security/browser/authentication/ImoocAuthenticationSuccessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
imooc-security-core/src/main/java/com/imooc/security/core/properties/LoginType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
imooc-security-demo/src/main/resources/resources/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |