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
9 changed files
with
227 additions
and
41 deletions.
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
86 changes: 86 additions & 0 deletions
86
.../src/main/java/com/imooc/security/app/validate/code/impl/RedisValidateCodeRepository.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,86 @@ | ||
/** | ||
* | ||
*/ | ||
package com.imooc.security.app.validate.code.impl; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
|
||
import com.imooc.security.core.validate.code.ValidateCode; | ||
import com.imooc.security.core.validate.code.ValidateCodeException; | ||
import com.imooc.security.core.validate.code.ValidateCodeRepository; | ||
import com.imooc.security.core.validate.code.ValidateCodeType; | ||
|
||
/** | ||
* @author zhailiang | ||
* | ||
*/ | ||
@Component | ||
public class RedisValidateCodeRepository implements ValidateCodeRepository { | ||
|
||
@Autowired | ||
private RedisTemplate<Object, Object> redisTemplate; | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see | ||
* com.imooc.security.core.validate.code.ValidateCodeRepository#save(org. | ||
* springframework.web.context.request.ServletWebRequest, | ||
* com.imooc.security.core.validate.code.ValidateCode, | ||
* com.imooc.security.core.validate.code.ValidateCodeType) | ||
*/ | ||
@Override | ||
public void save(ServletWebRequest request, ValidateCode code, ValidateCodeType type) { | ||
redisTemplate.opsForValue().set(buildKey(request, type), code, 30, TimeUnit.MINUTES); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see | ||
* com.imooc.security.core.validate.code.ValidateCodeRepository#get(org. | ||
* springframework.web.context.request.ServletWebRequest, | ||
* com.imooc.security.core.validate.code.ValidateCodeType) | ||
*/ | ||
@Override | ||
public ValidateCode get(ServletWebRequest request, ValidateCodeType type) { | ||
Object value = redisTemplate.opsForValue().get(buildKey(request, type)); | ||
if (value == null) { | ||
return null; | ||
} | ||
return (ValidateCode) value; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see | ||
* com.imooc.security.core.validate.code.ValidateCodeRepository#remove(org. | ||
* springframework.web.context.request.ServletWebRequest, | ||
* com.imooc.security.core.validate.code.ValidateCodeType) | ||
*/ | ||
@Override | ||
public void remove(ServletWebRequest request, ValidateCodeType type) { | ||
redisTemplate.delete(buildKey(request, type)); | ||
} | ||
|
||
/** | ||
* @param request | ||
* @param type | ||
* @return | ||
*/ | ||
private String buildKey(ServletWebRequest request, ValidateCodeType type) { | ||
String deviceId = request.getHeader("deviceId"); | ||
if (StringUtils.isBlank(deviceId)) { | ||
throw new ValidateCodeException("请在请求头中携带deviceId参数"); | ||
} | ||
return "code:" + type.toString().toLowerCase() + ":" + deviceId; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
imooc-security-app/src/main/java/com/imooc/security/app/validate/code/impl/package-info.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,8 @@ | ||
/** | ||
* | ||
*/ | ||
/** | ||
* @author zhailiang | ||
* | ||
*/ | ||
package com.imooc.security.app.validate.code.impl; |
66 changes: 66 additions & 0 deletions
66
...ain/java/com/imooc/security/browser/validate/code/impl/SessionValidateCodeRepository.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,66 @@ | ||
/** | ||
* | ||
*/ | ||
package com.imooc.security.browser.validate.code.impl; | ||
|
||
import org.springframework.social.connect.web.HttpSessionSessionStrategy; | ||
import org.springframework.social.connect.web.SessionStrategy; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
|
||
import com.imooc.security.core.validate.code.ValidateCode; | ||
import com.imooc.security.core.validate.code.ValidateCodeRepository; | ||
import com.imooc.security.core.validate.code.ValidateCodeType; | ||
|
||
/** | ||
* @author zhailiang | ||
* | ||
*/ | ||
@Component | ||
public class SessionValidateCodeRepository implements ValidateCodeRepository { | ||
|
||
/** | ||
* 验证码放入session时的前缀 | ||
*/ | ||
String SESSION_KEY_PREFIX = "SESSION_KEY_FOR_CODE_"; | ||
|
||
/** | ||
* 操作session的工具类 | ||
*/ | ||
private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy(); | ||
|
||
/* (non-Javadoc) | ||
* @see com.imooc.security.core.validate.code.ValidateCodeRepository#save(org.springframework.web.context.request.ServletWebRequest, com.imooc.security.core.validate.code.ValidateCode, com.imooc.security.core.validate.code.ValidateCodeType) | ||
*/ | ||
@Override | ||
public void save(ServletWebRequest request, ValidateCode code, ValidateCodeType validateCodeType) { | ||
sessionStrategy.setAttribute(request, getSessionKey(request, validateCodeType), code); | ||
} | ||
|
||
/** | ||
* 构建验证码放入session时的key | ||
* | ||
* @param request | ||
* @return | ||
*/ | ||
private String getSessionKey(ServletWebRequest request, ValidateCodeType validateCodeType) { | ||
return SESSION_KEY_PREFIX + validateCodeType.toString().toUpperCase(); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see com.imooc.security.core.validate.code.ValidateCodeRepository#get(org.springframework.web.context.request.ServletWebRequest, com.imooc.security.core.validate.code.ValidateCodeType) | ||
*/ | ||
@Override | ||
public ValidateCode get(ServletWebRequest request, ValidateCodeType validateCodeType) { | ||
return (ValidateCode) sessionStrategy.getAttribute(request, getSessionKey(request, validateCodeType)); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see com.imooc.security.core.validate.code.ValidateCodeRepository#remove(org.springframework.web.context.request.ServletWebRequest, com.imooc.security.core.validate.code.ValidateCodeType) | ||
*/ | ||
@Override | ||
public void remove(ServletWebRequest request, ValidateCodeType codeType) { | ||
sessionStrategy.removeAttribute(request, getSessionKey(request, codeType)); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...ity-browser/src/main/java/com/imooc/security/browser/validate/code/impl/package-info.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,8 @@ | ||
/** | ||
* | ||
*/ | ||
/** | ||
* @author zhailiang | ||
* | ||
*/ | ||
package com.imooc.security.browser.validate.code.impl; |
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
35 changes: 35 additions & 0 deletions
35
...rity-core/src/main/java/com/imooc/security/core/validate/code/ValidateCodeRepository.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,35 @@ | ||
/** | ||
* | ||
*/ | ||
package com.imooc.security.core.validate.code; | ||
|
||
import org.springframework.web.context.request.ServletWebRequest; | ||
|
||
/** | ||
* @author zhailiang | ||
* | ||
*/ | ||
public interface ValidateCodeRepository { | ||
|
||
/** | ||
* 保存验证码 | ||
* @param request | ||
* @param code | ||
* @param validateCodeType | ||
*/ | ||
void save(ServletWebRequest request, ValidateCode code, ValidateCodeType validateCodeType); | ||
/** | ||
* 获取验证码 | ||
* @param request | ||
* @param validateCodeType | ||
* @return | ||
*/ | ||
ValidateCode get(ServletWebRequest request, ValidateCodeType validateCodeType); | ||
/** | ||
* 移除验证码 | ||
* @param request | ||
* @param codeType | ||
*/ | ||
void remove(ServletWebRequest request, ValidateCodeType codeType); | ||
|
||
} |
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
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