- 浏览: 125483 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
x_root:
v11的注册机下载地址:http://download.csd ...
IntelliJIdea10 正式版发布了 -
x_root:
javax.ejb.TransactionAttributeT ...
说说ejb的事务传播机制 -
silenrain:
...
jqGrid使用总结 -
x_root:
目前最新版本是9.9,更新太快了。
XYplorer8.0发布了,一个好用的资源管理器 -
化蝶自在飞:
je的搜索确实有问题额.
为什么使用JE的搜索功能什么都搜不出来?
做权限控制的时候用了spring security2.0,但是后来又增加了登录页面需要验证码功能,想了一下,实现如下:
import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceAware; import org.springframework.context.support.MessageSourceAccessor; import org.springframework.security.AuthenticationException; import org.springframework.security.SpringSecurityMessageSource; import org.springframework.security.context.SecurityContextHolder; import org.springframework.security.ui.FilterChainOrder; import org.springframework.security.ui.SpringSecurityFilter; import org.springframework.security.util.RedirectUtils; import org.springframework.security.util.UrlUtils; import org.springframework.util.Assert; /** * 加入了验证码的过滤器 * * @author Alfoo Liu <liuxy2@xxx.com> * @since 2010-1-7 10:28:35 */ public class AuthenticationCodeFilter extends SpringSecurityFilter implements InitializingBean, MessageSourceAware { public final static String SPRING_SECURITY_FORM_AUTHCODE_KEY = "j_authCode"; private final static String AUTHCODE_KEY_IN_SESSION = "AUTHCODE"; public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "j_username"; public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME"; public static final String SPRING_SECURITY_LAST_EXCEPTION_KEY = "SPRING_SECURITY_LAST_EXCEPTION"; private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY; private String authCodeParameter = SPRING_SECURITY_FORM_AUTHCODE_KEY; private String authenticationFailureUrl = "/login.dm?error=true"; private boolean enableAuthCode = true; private final static Log log = LogFactory .getLog(AuthenticationCodeFilter.class); protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor(); private boolean useRelativeContext = false; private String filterProcessesUrl = "/j_spring_security_check"; @Override public void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { // Place the last username attempted into HttpSession for views HttpSession session = request.getSession(); // boolean s = session != null || getAllowSessionCreation(); // 获取页面传过来的验证码值 String authCode = this.obtainAuthCode(request); String username = obtainUsername(request); log.debug("auth code = " + authCode); if (enableAuthCode && requiresAuthentication(request, response)) { if (authCode == null || authCode.trim().length() == 0) { log.error("validate authcode failed, auth code is null."); session.setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, username); AuthenticationException exception = new AuthenticationCodeErrorException( messages .getMessage( "AbstractUserDetailsAuthenticationProvider.empty_authcode", "Please input authcode."));// "验证码不能为空。" unsuccessfulAuthentication(request, response, exception); return; } String expected = (String) session .getAttribute(AUTHCODE_KEY_IN_SESSION); if (!authCode.equals(expected)) {// AuthCodeValidationException session.setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, username); AuthenticationException exception = new AuthenticationCodeErrorException( messages .getMessage( "AbstractUserDetailsAuthenticationProvider.error_authcode", "authcode is error.")); unsuccessfulAuthentication(request, response, exception); return; } } chain.doFilter(request, response); } protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException { SecurityContextHolder.getContext().setAuthentication(null); if (logger.isDebugEnabled()) { logger .debug("Updated SecurityContextHolder to contain null Authentication"); } request.getSession().setAttribute( SPRING_SECURITY_LAST_EXCEPTION_KEY, failed); if (authenticationFailureUrl == null) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication Failed:" + failed.getMessage()); } else { sendRedirect(request, response, authenticationFailureUrl); } } protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) { String uri = request.getRequestURI(); int pathParamIndex = uri.indexOf(';'); if (pathParamIndex > 0) { // strip everything after the first semi-colon uri = uri.substring(0, pathParamIndex); } if ("".equals(request.getContextPath())) { return uri.endsWith(filterProcessesUrl); } return uri.endsWith(request.getContextPath() + filterProcessesUrl); } protected void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException { RedirectUtils.sendRedirect(request, response, url, useRelativeContext); } public int getOrder() { return FilterChainOrder.AUTHENTICATION_PROCESSING_FILTER - 100; } protected String obtainAuthCode(HttpServletRequest request) { return request.getParameter(getAuthCodeParameter()); } protected String obtainUsername(HttpServletRequest request) { return request.getParameter(usernameParameter); } public boolean isEnableAuthCode() { return enableAuthCode; } public void setEnableAuthCode(boolean enableAuthCode) { this.enableAuthCode = enableAuthCode; } public String getAuthCodeParameter() { return authCodeParameter; } public void setAuthCodeParameter(String authCodeParameter) { this.authCodeParameter = authCodeParameter; } public String getAuthenticationFailureUrl() { return authenticationFailureUrl; } public void setAuthenticationFailureUrl(String authenticationFailureUrl) { this.authenticationFailureUrl = authenticationFailureUrl; } public void setUseRelativeContext(boolean useRelativeContext) { this.useRelativeContext = useRelativeContext; } public void afterPropertiesSet() throws Exception { Assert.isTrue(UrlUtils.isValidRedirectUrl(authenticationFailureUrl), authenticationFailureUrl + " isn't a valid redirect URL"); } public void setMessageSource(MessageSource messageSource) { this.messages = new MessageSourceAccessor(messageSource); } }
applicationContext-security.xml配置如下:
<beans:bean id="authenticationProcessingFilter" class="com.asiainfo.webframe.core.security.AuthenticationCodeFilter"> <custom-filter before="AUTHENTICATION_PROCESSING_FILTER" /> <!--以下两项的配置应该要和form-login的配置相同--> <beans:property name="authenticationFailureUrl" value="/login.dm?error=true" /> </beans:bean>
http的配置仍然是auto-config="true"。
发表评论
-
这个是不是commons-logging的bug?
2011-09-29 17:47 979commons-logging-1.1.1.jar中的类org ... -
about commons-logging
2011-09-29 17:00 936system.getProperty("org.ap ... -
一个奇怪的问题
2011-08-31 15:37 786在用weblogic做性能测试的时候,跑了一段时间后,数据库的 ... -
说说ejb的事务传播机制
2011-05-13 11:18 2031说说ejb的事务传播机制 1、MANDATORY Suppor ... -
从xml中初始化bean实例
2011-03-28 20:14 1043需要从配置文件中初始化一个bean实例,并且里面需要包含一个M ... -
IntelliJIdea10 正式版发布了
2010-12-10 13:16 1135IntelliJIdea10 正式版本 终极版本发布了, 可 ... -
欢迎加入我的IntelliJ IDEA群
2010-10-14 09:59 170[url href="http://qun.qq.c ... -
Linux资料大全
2010-09-14 09:18 1530Linux经典书籍,全球开源社区集体智慧结晶,领略Linux内 ... -
为什么使用JE的搜索功能什么都搜不出来?
2010-05-14 17:51 1255如题,如下图: 奇了怪了,啥原因呢? -
jqGrid使用总结
2010-03-30 18:50 57371. 如何获取grid选中的行的ID? var rowid ... -
如何让idea的编辑器不允许虚拟空间
2009-07-13 16:43 1159如何让idea的编辑器不允许虚拟空间,即不能在行的末尾以外的位 ... -
掌握JAVA的标准
2009-06-22 13:52 822转自:http://www.java-cn.com ... -
sequence数据迁移
2009-06-16 13:39 971如何从另一台机器上的数据库用户迁移出sequence,以下是我 ... -
xml序列化的性能问题
2009-04-09 22:48 1548最近一个web模块在做性能测试,用lr一压,发现tps很低,还 ... -
ibatis的分页问题
2009-02-11 17:56 1026原来的queryForPaginatedList已经被废弃了, ... -
无聊的日期格式化
2009-01-20 13:07 850/** SimpleDateFormat函数语法: ... -
Freemarker内建变量
2009-01-14 18:41 1814变量解析/决定(Resolution) 在FreeMarke ... -
网络切换命令
2008-11-13 00:09 1223由于在家和在公司两个地方网络不一样,在家要设置固定IP和DNS ... -
如何使用Annotation
2008-11-12 22:46 1159经常看到别人使用自定义的Annotation,今天我也学了一把 ... -
ant 文件一般写法
2008-10-29 16:15 1129供自己参考: <?xml version="1 ...
相关推荐
本篇文章主要介绍了Spring Security Oauth2.0 实现短信验证码登录示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要介绍了Spring Security OAuth2集成短信验证码登录以及第三方登录,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring Security添加记住我功能 Spring Security短信验证码登录 Spring Security Session管理 Spring Security退出登录 Spring Security权限控制 Spring Security OAuth2入门 Spring Security OAuth2自定义Token获取...
基于SpringSecurity框架的表单/验证码/手机验证码登录方式. 基于SpringSocial的社交登录(qq和微信) 基于SpringSecurityOAuth2的,将自己作为认证服务器和资源服务器.并使用社交/表单/验证码/手机验证码登录方式 获取...
Java 安全框架 Spring Security 权限控制到按钮级别。 任务管理使用quartz 适配集成环境 WEB PC 管理端 表单组件完善 文件上传后回显 多条件显示 初始化引导,让新手都可以了如指掌 WEB PC UI全面更新 多模版选择 小...
项目简介代码拥有丰富的注释和,(文档尚在完善中,即将开放),基于springboot 2.0.4.RELEASE开发,后续有, 的重组计划,敬请期待主要功能如下: rabbitMQ + sendGrid初始化发送邮件,免费用户每月可发送10000条...
Java 安全框架 Spring Security 权限控制到按钮级别。 任务管理使用quartz 适配集成环境 WEB PC 管理端 表单组件完善 文件上传后回显 多条件显示 初始化引导,让新手都可以了如指掌 WEB PC UI全面更新 多模版选择 小...
服务介绍项目名称编号名称说明可乐云认证认证服务认证服务基于SpringSecurity进行安全认证,采用OAuth2.0认证体系,对客户端,用户进行认证及授权,支持账号密码登录,短信验证码登录可乐云配置配置配置服务基于
基于Spring Security Oauth2的统一认证服务,作为前端界面的 ,重写了Spring Security登录模式,支持初始化登录,所有接口以及授权端点都支持异步的方式。 支持特性 支持异步JSON登录 支持手机号,验证码登录 支持...
SAML2.0 SP DEMO,案例参见 https://github.com/MaxKeyTop/spring-boot-security-saml-sample docker支持的优化 源代码增加LICENSE声明 JWT的分离成protocols下子项目 密码策略提示参数问题修复 SAML 2.0 Metadata...
2、基于Spring Boot 2.0.X、Spring Cloud Finchley和Spring Cloud Alibaba 3、深度定制Spring Security真正实现了基于RBAC、jwt和oauth2的无状态统一权限认证的解决方案 4、提供应用管理,方便第三方系统接入 5、...
2、基于Spring Boot 2.0.X、Spring Cloud Finchley和Spring Cloud Alibaba 3、深度定制Spring Security真正实现了基于RBAC、jwt和oauth2的无状态统一权限认证的解决方案 4、提供应用管理,方便第三方系统接入 5、...
为 asp.net 2.0 的菜单控件增加 target 属性 ASP.NET 2.0 的内部变化 常见的 ASP.NET 2.0 转换问题和解决方案 Asp.Net2.0无刷新客户端回调 体验.net 2.0 的优雅(1) -- 异步WebService调用 ASP.NET 2.0页面框架的几...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...