项目需要做一个简单的SSO到我们用SpringSecurity2.0的系统,因为没有统一的用户LDAP,采用post用户名密码的方式。现在要处理的就是2件事,一是能够post通过验证,二是验证通过要跳转到指定的页面。一很好实现: http://localhost:8081/j_spring_security_check?j_username=admin&j_password=1,采用的默认配置就可以通过验证。简单看一下spring源码,AuthenticationProcessingFilter是默认filterChain中的一个,由它来处理form方式的验证,验证代码如下:
Java代码
public Authentication attemptAuthentication(HttpServletRequest request) throws AuthenticationException {
String username = obtainUsername(request);
String password = obtainPassword(request);
if (username == null) {
username = "";
}
if (password == null) {
password = "";
}
username = username.trim();
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
// Place the last username attempted into HttpSession for views
HttpSession session = request.getSession(false);
if (session != null || getAllowSessionCreation()) {
request.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, username);
}
// Allow subclasses to set the "details" property
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
public Authentication attemptAuthentication(HttpServletRequest request) throws AuthenticationException {
String username = obtainUsername(request);
String password = obtainPassword(request);
if (username == null) {
username = "";
}
if (password == null) {
password = "";
}
username = username.trim();
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
// Place the last username attempted into HttpSession for views
HttpSession session = request.getSession(false);
if (session != null || getAllowSessionCreation()) {
request.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, username);
}
// Allow subclasses to set the "details" property
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}Java代码
protected String obtainUsername(HttpServletRequest request) {
return request.getParameter(usernameParameter);
}
protected String obtainUsername(HttpServletRequest request) {
return request.getParameter(usernameParameter);
}Java代码
public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "j_username";
public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "j_password";
public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME";
public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "j_username";
public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "j_password";
public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME";Java代码
private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;
private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;Java代码
public String getDefaultFilterProcessesUrl() {
return "/j_spring_security_check";
}
public String getDefaultFilterProcessesUrl() {
return "/j_spring_security_check";
} url中的3个参数代码中都出现了。
第二个问题就复杂一点了,配置的默认页面是index.jsp,现在要跳转到其它页面。查不到文档,还是自己看源码。AuthenticationProcessingFilter extends了AbstractProcessingFilter,这个类有很多与配置对应的属性,一个我们需要的属性就是targetUrlResolver:
Java代码
private TargetUrlResolver targetUrlResolver = new TargetUrlResolverImpl();
private TargetUrlResolver targetUrlResolver = new TargetUrlResolverImpl(); 这里就是处理验证后的跳转,看默认的TargetUrlResolverImpl类实现:
Java代码
public String determineTargetUrl(SavedRequest savedRequest, HttpServletRequest currentRequest,
Authentication auth) {
String targetUrl = currentRequest.getParameter(targetUrlParameter);
if (StringUtils.hasText(targetUrl)) {
try {
return URLDecoder.decode(targetUrl, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported. Shouldn't be possible");
}
}
if (savedRequest != null) {
if (!justUseSavedRequestOnGet || savedRequest.getMethod().equals("GET")) {
targetUrl = savedRequest.getFullRequestUrl();
}
}
return targetUrl;
}
public String determineTargetUrl(SavedRequest savedRequest, HttpServletRequest currentRequest,
Authentication auth) {
String targetUrl = currentRequest.getParameter(targetUrlParameter);
if (StringUtils.hasText(targetUrl)) {
try {
return URLDecoder.decode(targetUrl, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported. Shouldn't be possible");
}
}
if (savedRequest != null) {
if (!justUseSavedRequestOnGet || savedRequest.getMethod().equals("GET")) {
targetUrl = savedRequest.getFullRequestUrl();
}
}
return targetUrl;
} 这里targetUrl也是先getParameter,这意味着通过url参数指定跳转页面成为了可能
Java代码
public static String DEFAULT_TARGET_PARAMETER = "spring-security-redirect";
private String targetUrlParameter = DEFAULT_TARGET_PARAMETER;
public static String DEFAULT_TARGET_PARAMETER = "spring-security-redirect";
private String targetUrlParameter = DEFAULT_TARGET_PARAMETER; 找到了这个参数,spring-security-redirect,尝试url:
http://localhost:8081/j_spring_security_check?j_username=admin&j_password=1&spring-security-redirect=draft.do
分享到:
相关推荐
spring security 基于oauth 2.0 实现 sso 单点登录Demo 使用 spring security 基于oauth 2.0 实现 sso 单点登录Demo spring boot + spring security + spring security oauth
Spring boot+Spring Security Oauth2.0,Sprint cloud+Spring Security Oauth2集成。四种认证方式。附带有代码,和案例,案例,还有视频链接。我保证看完就回,如果视频链接失效,评论回复我,我单独再给你一份。
使用 spring security 基于oauth 2.0 实现 sso 单点登录Demo spring boot + spring security + spring security oauth
spring security + oauth 2.0 实现单点登录、认证授权,直接贴代码
该资源实现了一个sso单点登陆的功能,类似于在淘宝网登陆之后可以不需要登陆天猫即可访问天猫网;使用了spring security oauth2以及jwt。
SpringCloud+SpringBoot+OAuth2+Spring Security+Redis实现的微服务统一认证授权
全网最新的Cloud 权限系统 基于Spring Boot 2.0.4.RELEASE ...基于 Spring Security oAuth 深度定制,支持社交登录等 完整的OAuth 2.0 流程,资源服务器控制权限 去除了部分对于开发不友好的中间件,快速上手
基于Spring Cloud、OAuth2.0的前后端分离的系统。 通用RBAC权限设计及其数据权限和分库分表 支持服务限流、动态路由、灰度发布、 支持常见登录方式, 多系统SSO登录 基于 Spring Cloud Hoxton 、Spring Boot 2.3、 ...
[SBS3] Spring Boot示例SAML 2.0服务提供程序项目描述该项目代表完全基于Spring Framework构建的SAML 2.0 ... 参考: : Spring Security SAML扩展Spring SAML扩展允许在Spring应用程序中无缝包含SAML 2.0服务提供程序功
Spring cloud security oauth sso 主要实现spring cloud security oauth2配置 access_token存放用户授权信息,通过redis关联,以达到授权服务与资源服务共享,防止token无效 enjoy-oauth2-authorization:授权服务 ,...
田鼠 Vole是一个基于最新的SpringCloud2.0的微服务商业开发脚手架->(非教学版),支持公司全方位应用架构搭建 产品特点 ...基于SpringSecurity的Cookie的sso统一登陆服务 支持分布式后台portal统一服务 支
基于SpringSecurity框架的表单/验证码/手机验证码登录方式. 基于SpringSocial的社交登录(qq和微信) 基于SpringSecurityOAuth2的,将自己作为认证服务器和资源服务器.并使用社交/表单/验证码/手机验证码登录方式 获取...
spring-saml-adfs 基于 Spring 的应用程序的 SSO 示例,用于通过 ADFS 2.0 与 Windows 域帐户集成。 这是基于上的 Spring Security SAML 项目。先决条件ADFS 2.0 正确安装在 Windows Server 机器上。 这充当 IdP。 ...
我解释了如何选择OAuth 2.0和MongoDB来构建可在微服务体系结构中使用的Single Sign On Auth服务器。 1.第一步。 运行sso-auth-client应用程序。 并尝试访问 。 不传递任何标题就返回此 2.第二步。 运行sso-auth-...
Security,该框架提供了一整套比较成熟,也很完整的机制用于处理各类场景下的可以基于权限,资源路径,以及授权方面的解决方案,部分模块支持定制化,而且在和oauth2.0进行了很好的无缝连接,在移动互联网的授权认证...
Security,该框架提供了一整套比较成熟,也很完整的机制用于处理各类场景下的可以基于权限,资源路径,以及授权方面的解决方案,部分模块支持定制化,而且在和oauth2.0进行了很好的无缝连接,在移动互联网的授权认证...
springboot2.0+oauth搭建的SSO单点登录的源码,仅仅实现登录功能,无需积分即可下载,如有问题请留言
OAuth2.0初探 学习编写 REST 接口测试用例 学习自定义 validator 学习自定义 filter 学习自定义 interceptor 自定义 aspect 学习 REST 接口多线程编程 Thread#start Thread#stop spring boot 1.5.20.RELEASE spring ...
基于Spring Security Oauth2的统一认证服务,作为前端界面的 ,重写了Spring Security登录模式,支持初始化登录,所有接口以及授权端点都支持异步的方式。 支持特性 支持异步JSON登录 支持手机号,验证码登录 支持...
HAP框架-SpringSecurity入门手册.docx HAP框架-SSO入门手册.docx HAP框架-定时调度器使用手册.docx HAP框架-开发规范.docx HAP框架-版本更新操作指引.docx HAP框架-附件功能使用手册.docx HETF-ActiveMQ入门手册.doc...