This is the sample to integrate SSO to Java web app with spring security, typical authentication process:
In this case, Web app sever is not proxyed, and recieve request directly, so the user's request arrive to Web app server firstly
Core Spring security config:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:sec="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<sec:http pattern="/assets/**" security="none" />
<sec:http pattern="/views/logout**" security="none" />
<bean id="userDetailsService" class="com.wilson.security.CustomUserDetailsService" />
<sec:http entry-point-ref="loginHandler" use-expressions="true" auto-config="false"
request-matcher="regex">
<sec:intercept-url pattern="/**" access="isAuthenticated()" />
<sec:logout logout-url="/logout" invalidate-session="true" success-handler-ref="logoutHandler" />
<sec:custom-filter ref="authenticationFilter" position="FORM_LOGIN_FILTER" />
</sec:http>
<bean id="loginHandler" class="com.wilson.security.SSOLoginHandler">
<property name="loginFormUrl" value="${sso.proxy}" />
<property name="authProcessingURL" value="http://${fqdn}:${port}${context}/authenticate" />
</bean>
<bean id="logoutHandler" class="com.wilson.security.SSOLogoutHandler">
<property name="defaultTargetUrl" value="/views/logout.jsp" />
<property name="alwaysUseDefaultTargetUrl" value="true" />
</bean>
<bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<constructor-arg value="/views/logout-failure.jsp"></constructor-arg>
</bean>
<bean id="authenticationFilter" class="com.wilson.security.SSOAuthenticationFilter">
<property name="filterProcessesUrl" value="/authenticate" />
<property name="authenticationManager" ref="authenticationManager" />
<property name="AuthenticationFailureHandler" ref="authenticationFailureHandler" />
<property name="publicKey" value="${sso.publickey}" />
</bean>
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider user-service-ref="userDetailsService" />
</sec:authentication-manager>
</beans>
So, first, we need to add a custom LoginUrlAuthenticationEntryPoint to redirect request to SSO when user open home page without login
public class SSOLoginHandler extends LoginUrlAuthenticationEntryPoint
{
private final Logger logger = LoggerFactory.getLogger(SSOLoginHandler.class);
private String authProcessingURL;
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response,
final AuthenticationException authenticationException) throws IOException, ServletException
{
logger.debug("Preparing redirectiion to SSO PROXY...");
// new DefaultRedirectStrategy().sendRedirect(request, response, this.getLoginFormUrl() + "?ref="
// + authProcessingURL);
String SSO_LOGIN_URL= "https://ssoserver.com/sso.jsp";
new DefaultRedirectStrategy().sendRedirect(request, response, SSO_LOGIN_URL + "?ref="
+ authProcessingURL);
}
public String getAuthProcessingURL()
{
return authProcessingURL;
}
public void setAuthProcessingURL(final String authProcessingURL)
{
this.authProcessingURL = authProcessingURL;
}
Simplete logout which do some logging items..
public class SSOLogoutHandler extends SimpleUrlLogoutSuccessHandler
{
private final Logger logger = LoggerFactory.getLogger(SSOLogoutHandler.class);
@Override
public void onLogoutSuccess(final HttpServletRequest request, final HttpServletResponse response,
final Authentication authentication) throws IOException, ServletException
{
super.onLogoutSuccess(request, response, authentication);
logger.debug("Performing an SSO logout at: {}", this.getDefaultTargetUrl());
}
}
Custom UserDeatailsService to load role and Grant Authority to user
public class CustomUserDetailsService implements UserDetailsService
{
public static final String DEFAULT_AUTH_PASSWORD = "password";
@Override
public UserDetails loadUserByUsername(String soeid) throws UsernameNotFoundException
{
List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
grantedAuths.add(new SimpleGrantedAuthority(***Service.queryUserRoleFromDatabase(soeid).toString()));
UserDetails user = new User(soeid, DEFAULT_AUTH_PASSWORD, true, true, true, true, grantedAuths);
return user;
}
}
Custom authetication filter to processe the response form SSO server after logicn
public class SSOAuthenticationFilter extends UsernamePasswordAuthenticationFilter
{
public static final String DEFAULT_AUTH_PASSWORD = "password";
private final Logger logger = LoggerFactory.getLogger(SSOAuthenticationFilter.class);
private Cipher cipher;
public SSOAuthenticationFilter()
{
super.setPostOnly(false); // allow a GET request from SSO PROXY
}
@Override
public Authentication attemptAuthentication(final HttpServletRequest request, final HttpServletResponse response) throws AuthenticationException
{
String[] sid = decodeSID(request);
String soeid = sid[0];
// token is expired if currentTimeMillis is greater then TIMESTAMP
if (System.currentTimeMillis() > Long.parseLong(sid[1]))
{
logger.error("Authentication rejected for: {}", soeid);
throw new NonceExpiredException("Authentication token is expired");
}
// saving decoded SOEID in a REQUEST to reuse it by obtainUsername()
request.setAttribute("SSO_USER_SOEID", soeid);
return super.attemptAuthentication(request, response);
}
@Override
protected String obtainPassword(final HttpServletRequest request)
{
return DEFAULT_AUTH_PASSWORD;
}
@Override
protected String obtainUsername(final HttpServletRequest request)
{
//SM_USER is coming from SSO after login
return (String) request.getAttribute("SM_USER");
}
private String[] decodeSID(final HttpServletRequest request)
{
.............add SSO server decode strtegy
}
}
You may say above sample was not my case, what happens we have SSO setup in the proxy server as below?
only difference is in the login entry filter, we redirect to the web app authentication filter("/authenticate") as it's pre-logged in
public class SSOLoginHandler extends LoginUrlAuthenticationEntryPoint
{
private final Logger logger = LoggerFactory.getLogger(SSOLoginHandler.class);
private String authProcessingURL;
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response,
final AuthenticationException authenticationException) throws IOException, ServletException
{
logger.debug("Preparing redirectiion to SSO PROXY...");
new DefaultRedirectStrategy().sendRedirect(request, response, "/authenticate");
}
}
- 大小: 142.2 KB
- 大小: 16.5 KB
分享到:
相关推荐
Architect solutions that leverage the full power of Spring Security while remaining loosely coupled. Implement various ... You are not expected to have any previous experience with Spring Security.
Hands-On Spring Security 5 for Reactive Applications starts with the essential concepts of reactive programming, Spring Framework, and Spring Security. You will then learn about a variety of ...
其它下载pdf书签部分链接不正确,我...Integrate home monitoring and security notifications with monitoring systems Use logical level shifter with Arduino to send and receive data to and from Raspberry PI
- BIEE 10.1.3.2 版本,已安装Advanced Security选项,它包含Web Bridge servlet、JMX BeanServer和Oracle BI Publisher组件,这些组件在Oracle Application Server 10.1.3上运行,而不是默认的OC4J。 - Oracle ...
Kotlin is being used ... You’ll also learn to use Spring Security to beef up security of your application before testing it with the JUnit framework and then deploying it on a cloud platform like AWS.
apach module for integrate with tomcat
Here comes an example to show how BIRT integrate with RCP,This is not a complete project,But snippets are foundation to built large projects. It's worth learning.
solution approach, Spring Boot 2 Recipes quickly introduces you to Pivotal's Spring Boot 2 micro-framework, then dives into code snippets on how to apply and integrate Spring Boot 2 with the Spring ...
Also, Spring can integrate effortlessly with other popular web frameworks such as Struts, WebWork, Java Server Faces, and Tapestry. The book progressively teaches you to configure the Spring ...
Compile to integrate with whatsap, receive and send direct msg in your application delphi fully full component. see the video below for you to see how the component works ...
Pro RESTful APIs Design Build and Integrate with REST JSON XML and JAX-RS 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn...
Spring Batch PoC Grails Spring Batch、启用 CORS 的 REST API、Spring Security Core/LDAP 演示先决条件 # GVM - Tool for managing Groovy related frameworks curl -s get.gvmtool.net | bash # install Grails ...
You will learn and integrate these messaging APIs with more complex enterprise and cloud applications: for example, you will see how to use Spring Cloud Stream for creating message-driven and cloud ...
Get started with Spring Framework 5 and its ecosystem, with a guide to the working practices in modern development. This book will teach you how to use the Spring Framework to build Java-based ...
3. **安全控制**:Spring Security是Spring Boot默认的安全解决方案,提供了认证、授权、CSRF保护等功能。 4. **邮件服务**:通过JavaMailSender接口,Spring Boot可以方便地发送电子邮件。 5. **缓存管理**:...
Integrate Azure Security Center with Azure AD Identity Protection Center and third-party solutions Adapt Azure Security Center’s built-in policies and definitions for your organization Perform ...