SlideShare a Scribd company logo
Securing Portlets With Spring Security John A. Lewis Chief Software Architect Unicon, Inc. JA-SIG Spring 2008 Conference 28 April 2008 © Copyright Unicon, Inc., 2007.  Some rights reserved.  This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. To view a copy of this license, visit  http://creativecommons.org/licenses/by-nc-sa/3.0/us/
Agenda JSR 168 Portlet Security Spring Security (aka “Acegi”) Spring Portlet Security Applying Portlet Security Resources Questions & Answers
JSR 168 Portlet Security What does the spec give us to work with?
Portal Authentication The portal is completely responsible for authentication This means we just use what it gives us – we don't redirect for authentication purpose The JSR 168  PortletRequest  class provides two methods for getting user identity (the same ones as the Servlet spec) String getRemoteUser()  Principal getUserPrincipal()
Portal Authorization Portals generally provide the ability to assign a set of “Roles” to the User The JSR 168  PortletRequest  class provides a method for getting at these roles (the same ones as the Servlet spec) boolean isUserInRole(String)
Declaring Portal Roles Same as declaring roles for Servlet container-based security Include all portal roles that may be used in  web.xml : ... <security-role> <role-name>manager</role-name> </security-role> ...
Mapping Portal Roles To Portlet Roles In  portlet.xml : <portlet> <portlet-name>books</portlet-name> ... <security-role-ref> <role-name>ADMINISTRATOR</role-name> <role-link>manager</role-link> </security-role-ref> </portlet> Portlet Role Portal Role Warning! If you are storing your  SecurityContext  in the  PortletSession  with  APPLICATION_SCOPE  (more on this later) , make sure these are the same in all your  <portlet>  declarations – the first one to be invoked on a page will determine the mapping for all portlets in your webapp.
Security Constraints Require a secure transport in  portlet.xml : <portlet-app> ... <portlet> <portlet-name>accountSummary</portlet-name> ... </portlet> ... <security-constraint> <display-name>Secure Portlets</display-name> <portlet-collection> <portlet-name>accountSummary</portlet-name> </portlet-collection> <user-data-constraint/> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> ... </portlet-app>
Other Portlet Security Info PortletRequest  has a couple other key security-related methods: String getAuthType() Returns name of authentication scheme used (BASIC_AUTH, CLIENT_CERT_AUTH, custom) or null if user is not authenticated. boolean isSecure() Returns true if the request was made over a secure channel (such as HTTPS) String getAuthType() boolean isSecure()
Portlet User Attributes Can also use the  USER_INFO  Map available as a  PortletRequest  attribute. May contain arbitrary user information: user.name.given user.bdate user.gender etc. Some portals expose security-related information here, but this mechanism should be avoided if possible
Spring Security a.k.a  Acegi Security A quick overview
What Is Spring Security? Powerful, flexible security framework  for enterprise software Emphasis on applications using Spring Comprehensive authentication, authorization, and instance-based access control Avoids security code in your business logic – treats security as a cross-cutting concern Built-in support for a wide variety of authentication and integration standards
Spring Security Releases Acegi Security  (the old name) Current Version: 1.0.7 Initial GA Release: May 2006 Portlet support in Sandbox Spring Security  (the new name) Current Version: 2.0.0 Initial GA Release: April 2008 Portlet support Included Changes packaging from  org.acegisecurity   to  org.springframework.security
Applications Are Like Onions Spring Security can be applied at multiple layers in your application: Apply security as markup is constructed in the  Rendering Layer  using the supplied JSP taglib Restrict access to areas of web application in the  Dispatch Layer  based on URL pattern-matching Secure method invocation on the  Service Layer  to ensure calls are from properly authorized user Provide Access Control Lists (ACLs) for individual objects in the  Domain Layer
Spring Portlet Security Applying Spring Security to Portlets
Portlet Challenges Portlets have some key differences  from Servlets: No Filters Can't treat URLs like Paths Multiple Request Phases These create some challenges in applying the normal Spring Security patterns So we need some different infrastructure for wiring Spring Security into our portlet application
Six Main Portlet Security Beans PortletProcessingInterceptor AuthenticationManager AuthenticationDetailsSource AuthenticationProvider UserDetailsService PortletSessionContextIntegrationInterceptor
PortletProcessingInterceptor <bean id=&quot;portletProcessingInterceptor&quot; class=&quot;org.springframework.security.ui.portlet. PortletProcessingInterceptor&quot;> <property name=&quot;authenticationManager&quot; ref=&quot;authenticationManager&quot; /> <property name=&quot;authenticationDetailsSource&quot; ref=&quot;portletAuthenticationDetailsSource&quot; / > </bean> Interceptor that processes portlet requests for authentication by invoking the configured  AuthenticationManager Creates the initial  AuthenticationToken  from the  PortletRequest  security methods Portlet equivalent of  AuthenticationProcessingFilter  used for traditional servlet web applications
AuthenticationManager <sec:authentication-manager  alias=&quot;authenticationManager&quot; /> Use normal provider-based  AuthenticationManager  bean Declared via special namespace schema: Can use multiple providers if you are authenticating from Portlets and Servlets
AuthenticationDetailsSource <bean name=” portletAuthenticationDetailsSource” class=&quot;org.springframework.security.ui.portlet. PortletPreAuthenticatedAuthenticationDetailsSource&quot;> <property name=&quot;mappableRolesRetriever&quot;> <bean class=&quot;org.springframework.security. authoritymapping.SimpleMappableAttributesRetriever&quot;> <property name=&quot;mappableAttributes&quot;> <list> <value>ADMIN</value> </list> </property> </bean> </property> </bean> Can be used to check isUserInRole(...) to get list of Portal Roles into the Authentication Request:  Only needed if we are using Portal Roles for our security decisions
AuthenticationProvider <bean id=&quot;portletAuthenticationProvider&quot; class=&quot;org.springframework.security.providers.preauth. PreAuthenticatedAuthenticationProvider&quot;> <sec:custom-authentication-provider /> <property name=&quot;preAuthenticatedUserDetailsService&quot; ref=&quot; preAuthenticatedUserDetailsService &quot; /> </bean> PreAuthenticatedAuthenticationProvider processes pre-authenticated authentication request (from  PortletProcessingInterceptor ) A valid  PreAuthenticatedAuthenticationToken  with non-null principal & credentials will succeed
UserDetailsService <bean  name=&quot;preAuthenticatedUserDetailsService&quot; class=&quot;org.springframework.security.providers.preauth. PreAuthenticatedGrantedAuthoritiesUserDetailsService&quot; /> Bean that knows how to populate user details (including  GrantedAuthorities ) for the authenticated user PreAuthenticatedGrantedAuthoritiesUserDetailsService  will use purely data contained in the  PreAuthenticatedAuthenticationToken Can also use any other  UserDetailsService  that can populate  UserDetails  by username, such as  JdbcUserDetailsManager  or  LdapUserDetailsManager
PortletSessionContextIntegrationInterceptor <bean id=&quot;portletSessionContextIntegrationInterceptor&quot; class=&quot;org.springframework.security.context. PortletSessionContextIntegrationInterceptor&quot; /> Interceptor that retrieves/stores the contents of the  SecurityContextHolder  in the active  PortletSession Without this, every request would trigger a full authentication cycle Default is to use  APPLICATION_SCOPE Portlet equivalent of  HttpSessionContextIntegrationFilter,  used for traditional servlet web applications
Using The Two Interceptors <bean id=&quot;portletModeHandlerMapping&quot; class=&quot;org.springframework.web.portlet.handler. PortletModeHandlerMapping&quot;> <property name=&quot;interceptors&quot;> <list> <ref bean=&quot; portletSessionContextIntegrationInterceptor &quot;/> <ref bean=&quot; portletProcessingInterceptor &quot;/> </list> </property> <property name=&quot;portletModeMap&quot;> <map> <entry key=&quot;view&quot;><ref bean=&quot;viewController&quot;/></entry> <entry key=&quot;edit&quot;><ref bean=&quot;editController&quot;/></entry> <entry key=&quot;help&quot;><ref bean=&quot;helpController&quot;/></entry> </map> </property> </bean> Add them to our Portlet's  HandlerMapping : Warning!  This ordering is critical – they will not work correctly if they are reversed!
Applying Portlet Security To The Rendering Layer Customizing our markup  based on security information
Spring Security JSP TagLib <%@ taglib prefix=&quot;sec&quot;  uri=&quot;http://www.springframework.org/security/tags&quot; %> <p>Username: <sec:authentication property=&quot;principal.username&quot;/></p> < sec :authorize ifAllGranted=&quot;ROLE_USER&quot;> <p>You are an authorized user of this system.</p> </ sec :authorize> <sec:authorize ifAllGranted=&quot;ROLE_ADMINISTRATOR&quot;> <p>You are an administrator of this system.</p> </sec:authorize> Allows us to access authentication information and to check authorizations Useful for showing/hiding information or navigation controls based on security info Warning:  Don't rely on this to restrict access to areas of the application.  Just because navigation doesn't appear in the markup doesn't mean a clever hacker can't generate a GET/POST that will still get there.
Applying Portlet Security To The Dispatch Layer Controlling where users can go in the application
Secure Portlet Request Dispatching Portlet Requests don't have a path structure, so we can't use the path-based patterns of  FilterSecurityInterceptor   to control access Something standard may be added in the future – perhaps a  ConfigAttributeDefinition  for various aspects of Portlet Requests that we can use as an  ObjectDefinitionSource
Using a  HandlerInterceptor Best practice in Spring 2.0 is to build a custom  HandlerInterceptor  for your Portlet Compare contents of  SecurityContextHolder. getContext(). getAuthentication()  with Portlet Mode, Window State, Render Parameters – whatever you want to use to determine permission Throw a  PortletSecurityException  if access is not permitted, otherwise allow processing to proceed
Using Annotations If using Spring 2.5 Annotation-based Dispatching, use Security Annotations as well ApplicationContext entry: Annotated method: <sec:global-method-security secured-annotations=&quot;enabled&quot; /> import org.springframework.security.annotation.Secured; ... @Secured({&quot;ROLE_ADMIN&quot;}) @RequestMapping(params=&quot;action=view&quot;) public String deleteItems(RequestParam(&quot;item&quot;) int itemId) { ...
Applying Portlet Security To The Service Layer Making sure Services are invoked by only by user with proper permissions
AccessDecisionManager <bean id=&quot;accessDecisionManager&quot; class=&quot;org.springframework.security.vote. AffirmativeBased&quot;> <property name=&quot;decisionVoters&quot;> <list> <bean class=&quot;org.springframework.security. vote.RoleVoter&quot; /> <bean class=&quot;org.springframework.security. vote.AuthenticatedVoter&quot; /> </list> </property> </bean> Standard Spring Security bean for making decisions about access to resources
MethodSecurityInterceptor <bean id=&quot;myService&quot; class=&quot;sample.service.MyService&quot;> <sec:intercept-methods access-decision-manager-ref=&quot;accessDecisionManager&quot;> <sec:protect method=&quot;sample.service.MyService.*&quot;  access=&quot;IS_AUTHENTICATED_FULLY&quot; /> <sec:protect method=&quot;sample.service.MyService.add*&quot; access=&quot;ROLE_ADMINISTRATOR&quot; /> <sec:protect method=&quot;sample.service.MyService.del*&quot; access=&quot;ROLE_ADMINISTRATOR&quot; /> <sec:protect method=&quot;sample.service.MyService.save*&quot; access=&quot;ROLE_ADMINISTRATOR&quot; /> </sec:intercept-methods> </bean>
Applying Portlet Security To Servlets Using the whole web/portlet application  as one secure bundle
Bridging The Gap We can reuse the Portlet  SecurityContext  in getting resources from Servlets in the same web application Useful for securing: AJAX Calls Dynamic Images PDF Reports Need to get Portlets and Servlets to share session data to do this
Portlets & Servlets Sharing Session Possible according to JSR 168 (PLT 15.4) Must be in the same webapp Portlet must use  APPLICATION_SCOPE Sometime tricky in practice Portlet requests go thru Portal webapp URL Servlet requests go thru Portlet webapp URL Session tracking via  JSESSIONID  Cookie usually uses URL path to webapp – not shared! Tomcat 5.5.4 + On  <Connector>  element set  emptySessionPath=true
Apply Servlet Filter Chain In  web.xml : <filter> <filter-name>securityFilterChainProxy</filter-name> <filter-class>org.springframework.web.filter. DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>securityFilterChainProxy</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
FilterChainProxy Since the portal handles authentication, you only need a few entries in this   bean : <bean id=&quot;servletSecurityFilterChainProxy&quot; class=&quot;org.springframework.security.util. FilterChainProxy&quot;> <sec:filter-chain-map path-type=&quot;ant&quot;> <sec:filter-chain pattern=&quot;/**&quot; filters=&quot;httpSessionContextIntegrationFilter, exceptionTranslationFilter, filterSecurityInterceptor&quot; /> </sec:filter-chain-map> </bean>
HttpSessionContextIntegrationFilter If session sharing is working properly, it will populate the SecurityContextHolder using the same SecurityContext as the Portlet side <bean id=&quot;httpSessionContextIntegrationFilter&quot; class=&quot;org.springframework.security.context. HttpSessionContextIntegrationFilter&quot; /> This will only work if  PortletSessionContextIntegrationInterceptor  is storing in the  APPLICATION_SCOPE  of the  PortletSession (which is the default)
ExceptionTranslationFilter Since we are relying on the Portal for authentication, then an Exception means that authentication has already failed PreAuthenticatedProcessingFilterEntryPoint  returns  SC_FORBIDDEN  (HTTP 403 error) <bean id=&quot;exceptionTranslationFilter&quot; class=&quot;org.springframework.security.ui. ExceptionTranslationFilter&quot;> <property name=&quot;authenticationEntryPoint&quot;> <bean class=&quot;org.springframework.security.ui.preauth. PreAuthenticatedProcessingFilterEntryPoint&quot; /> </property> </bean>
FilterSecurityInterceptor Secure resource URLs accordingly Use the same  AuthenticationManager  and  AccessDecisionManager  as in the portlet <bean id=&quot;filterSecurityInterceptor&quot; class=&quot;org.springframework.security.intercept.web. FilterSecurityInterceptor&quot;> <property name=&quot;authenticationManager&quot; ref=&quot;authenticationManager&quot; /> <property name=&quot;accessDecisionManager&quot; ref=&quot;accessDecisionManager&quot; /> <property name=&quot;objectDefinitionSource&quot;> <sec:filter-invocation-definition-source> <sec:intercept-url pattern=&quot;/resources/**&quot;  access=&quot;IS_AUTHENTICATED_FULLY&quot; /> </sec:filter-invocation-definition-source> </property> </bean>
Resources Places to go to actually use this stuff!
Resources Spring Security 2.0 Website http://static.springframework.org/spring-security/site/ Sample Applications Small sample included in Spring Security distro Bigger sample on the  Spring Portlet Wiki http://opensource.atlassian.com/confluence/spring/display/JSR168/
Questions & Answers John A. Lewis Chief Software Architect Unicon, Inc. [email_address] www.unicon.net

More Related Content

PDF
Portlet Specification 3.0 Is Here!
ODP
Java Portlet 2.0 (JSR 286) Specification
PPTX
Introduction to java standard portlets
PPT
Servlet 3.0
PPT
Portlets 2.0 JSR286
ODP
Developing JSR 286 Portlets
PPT
Spring 3.x - Spring MVC
PDF
Java EE 7: Boosting Productivity and Embracing HTML5
Portlet Specification 3.0 Is Here!
Java Portlet 2.0 (JSR 286) Specification
Introduction to java standard portlets
Servlet 3.0
Portlets 2.0 JSR286
Developing JSR 286 Portlets
Spring 3.x - Spring MVC
Java EE 7: Boosting Productivity and Embracing HTML5

What's hot (20)

PDF
securing-portlets-with-spring-security.pdf
PPT
JSF Component Behaviors
PPT
Java Server Faces (JSF) - advanced
PPT
Listeners and filters in servlet
ODP
Annotation-Based Spring Portlet MVC
PPT
Spring MVC
ODP
Spring Portlet MVC
PDF
Java EE 7: Whats New in the Java EE Platform @ Devoxx 2013
ODP
JSF 2.0 (JavaEE Webinar)
ODP
A Complete Tour of JSF 2
PDF
Identifing Listeners and Filters
PDF
Java Web Programming [9/9] : Web Application Security
PDF
Jsf intro
PPTX
Servlets - filter, listeners, wrapper, internationalization
PPTX
Rest with Java EE 6 , Security , Backbone.js
PPT
Introduction To ASP.NET MVC
PDF
Java Web Programming [8/9] : JSF and AJAX
securing-portlets-with-spring-security.pdf
JSF Component Behaviors
Java Server Faces (JSF) - advanced
Listeners and filters in servlet
Annotation-Based Spring Portlet MVC
Spring MVC
Spring Portlet MVC
Java EE 7: Whats New in the Java EE Platform @ Devoxx 2013
JSF 2.0 (JavaEE Webinar)
A Complete Tour of JSF 2
Identifing Listeners and Filters
Java Web Programming [9/9] : Web Application Security
Jsf intro
Servlets - filter, listeners, wrapper, internationalization
Rest with Java EE 6 , Security , Backbone.js
Introduction To ASP.NET MVC
Java Web Programming [8/9] : JSF and AJAX
Ad

Similar to Securing Portlets With Spring Security (20)

PDF
securing-portlets-with-spring-security.pdf
PPT
Implementation of ssl injava
PPT
Automated Testing Of Web Applications Using XML
PPT
JavaEE Security
PPT
Implementing application security using the .net framework
PDF
Java EE Application Security With PicketLink
PPT
Getting Started with Enterprise Library 3.0 in ASP.NET
PDF
Spring security jwt tutorial toptal
PPT
Spring training
PPT
Application Security
PPT
Bh Win 03 Rileybollefer
PPTX
Spring Security services for web applications
PPT
Incorporating Web Services in Mobile Applications - Web 2.0 San Fran 2009
PPTX
SgCodeJam24 Workshop Extract
PPT
UserCentric Identity based Service Invocation
PPTX
Claims Based Identity In Share Point 2010
ODP
SCWCD 2. servlet req - resp (cap3 - cap4)
PPTX
Lightning Component - Components, Actions and Events
PDF
Apache Roller, Acegi Security and Single Sign-on
PPT
Struts2.0basic
securing-portlets-with-spring-security.pdf
Implementation of ssl injava
Automated Testing Of Web Applications Using XML
JavaEE Security
Implementing application security using the .net framework
Java EE Application Security With PicketLink
Getting Started with Enterprise Library 3.0 in ASP.NET
Spring security jwt tutorial toptal
Spring training
Application Security
Bh Win 03 Rileybollefer
Spring Security services for web applications
Incorporating Web Services in Mobile Applications - Web 2.0 San Fran 2009
SgCodeJam24 Workshop Extract
UserCentric Identity based Service Invocation
Claims Based Identity In Share Point 2010
SCWCD 2. servlet req - resp (cap3 - cap4)
Lightning Component - Components, Actions and Events
Apache Roller, Acegi Security and Single Sign-on
Struts2.0basic
Ad

More from John Lewis (12)

PDF
Jasig uMobile - Open Source Enterprise Mobile Campus Solution
ODP
IMS LIS Outcomes and Sakai: Standardizing Grade Exchange
ODP
New Opportunites to Connect Learning with LIS and LTI
ODP
Open Source Your Project (With Jasig)
ODP
Sakai uPortal Integration Options
ODP
Sprint Portlet MVC Seminar
ODP
Agile Engineering
ODP
Scrum Process
ODP
Shibboleth Guided Tour Webinar
ODP
Leveraging Open Source
ODP
Open Source Licensing
PDF
Real World Identity Managment
Jasig uMobile - Open Source Enterprise Mobile Campus Solution
IMS LIS Outcomes and Sakai: Standardizing Grade Exchange
New Opportunites to Connect Learning with LIS and LTI
Open Source Your Project (With Jasig)
Sakai uPortal Integration Options
Sprint Portlet MVC Seminar
Agile Engineering
Scrum Process
Shibboleth Guided Tour Webinar
Leveraging Open Source
Open Source Licensing
Real World Identity Managment

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
KodekX | Application Modernization Development
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Approach and Philosophy of On baking technology
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation_ Review paper, used for researhc scholars
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KodekX | Application Modernization Development
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
Approach and Philosophy of On baking technology
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
The Rise and Fall of 3GPP – Time for a Sabbatical?
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Chapter 3 Spatial Domain Image Processing.pdf
MYSQL Presentation for SQL database connectivity
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation_ Review paper, used for researhc scholars

Securing Portlets With Spring Security

  • 1. Securing Portlets With Spring Security John A. Lewis Chief Software Architect Unicon, Inc. JA-SIG Spring 2008 Conference 28 April 2008 © Copyright Unicon, Inc., 2007. Some rights reserved. This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/
  • 2. Agenda JSR 168 Portlet Security Spring Security (aka “Acegi”) Spring Portlet Security Applying Portlet Security Resources Questions & Answers
  • 3. JSR 168 Portlet Security What does the spec give us to work with?
  • 4. Portal Authentication The portal is completely responsible for authentication This means we just use what it gives us – we don't redirect for authentication purpose The JSR 168 PortletRequest class provides two methods for getting user identity (the same ones as the Servlet spec) String getRemoteUser() Principal getUserPrincipal()
  • 5. Portal Authorization Portals generally provide the ability to assign a set of “Roles” to the User The JSR 168 PortletRequest class provides a method for getting at these roles (the same ones as the Servlet spec) boolean isUserInRole(String)
  • 6. Declaring Portal Roles Same as declaring roles for Servlet container-based security Include all portal roles that may be used in web.xml : ... <security-role> <role-name>manager</role-name> </security-role> ...
  • 7. Mapping Portal Roles To Portlet Roles In portlet.xml : <portlet> <portlet-name>books</portlet-name> ... <security-role-ref> <role-name>ADMINISTRATOR</role-name> <role-link>manager</role-link> </security-role-ref> </portlet> Portlet Role Portal Role Warning! If you are storing your SecurityContext in the PortletSession with APPLICATION_SCOPE (more on this later) , make sure these are the same in all your <portlet> declarations – the first one to be invoked on a page will determine the mapping for all portlets in your webapp.
  • 8. Security Constraints Require a secure transport in portlet.xml : <portlet-app> ... <portlet> <portlet-name>accountSummary</portlet-name> ... </portlet> ... <security-constraint> <display-name>Secure Portlets</display-name> <portlet-collection> <portlet-name>accountSummary</portlet-name> </portlet-collection> <user-data-constraint/> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> ... </portlet-app>
  • 9. Other Portlet Security Info PortletRequest has a couple other key security-related methods: String getAuthType() Returns name of authentication scheme used (BASIC_AUTH, CLIENT_CERT_AUTH, custom) or null if user is not authenticated. boolean isSecure() Returns true if the request was made over a secure channel (such as HTTPS) String getAuthType() boolean isSecure()
  • 10. Portlet User Attributes Can also use the USER_INFO Map available as a PortletRequest attribute. May contain arbitrary user information: user.name.given user.bdate user.gender etc. Some portals expose security-related information here, but this mechanism should be avoided if possible
  • 11. Spring Security a.k.a Acegi Security A quick overview
  • 12. What Is Spring Security? Powerful, flexible security framework for enterprise software Emphasis on applications using Spring Comprehensive authentication, authorization, and instance-based access control Avoids security code in your business logic – treats security as a cross-cutting concern Built-in support for a wide variety of authentication and integration standards
  • 13. Spring Security Releases Acegi Security (the old name) Current Version: 1.0.7 Initial GA Release: May 2006 Portlet support in Sandbox Spring Security (the new name) Current Version: 2.0.0 Initial GA Release: April 2008 Portlet support Included Changes packaging from org.acegisecurity to org.springframework.security
  • 14. Applications Are Like Onions Spring Security can be applied at multiple layers in your application: Apply security as markup is constructed in the Rendering Layer using the supplied JSP taglib Restrict access to areas of web application in the Dispatch Layer based on URL pattern-matching Secure method invocation on the Service Layer to ensure calls are from properly authorized user Provide Access Control Lists (ACLs) for individual objects in the Domain Layer
  • 15. Spring Portlet Security Applying Spring Security to Portlets
  • 16. Portlet Challenges Portlets have some key differences from Servlets: No Filters Can't treat URLs like Paths Multiple Request Phases These create some challenges in applying the normal Spring Security patterns So we need some different infrastructure for wiring Spring Security into our portlet application
  • 17. Six Main Portlet Security Beans PortletProcessingInterceptor AuthenticationManager AuthenticationDetailsSource AuthenticationProvider UserDetailsService PortletSessionContextIntegrationInterceptor
  • 18. PortletProcessingInterceptor <bean id=&quot;portletProcessingInterceptor&quot; class=&quot;org.springframework.security.ui.portlet. PortletProcessingInterceptor&quot;> <property name=&quot;authenticationManager&quot; ref=&quot;authenticationManager&quot; /> <property name=&quot;authenticationDetailsSource&quot; ref=&quot;portletAuthenticationDetailsSource&quot; / > </bean> Interceptor that processes portlet requests for authentication by invoking the configured AuthenticationManager Creates the initial AuthenticationToken from the PortletRequest security methods Portlet equivalent of AuthenticationProcessingFilter used for traditional servlet web applications
  • 19. AuthenticationManager <sec:authentication-manager alias=&quot;authenticationManager&quot; /> Use normal provider-based AuthenticationManager bean Declared via special namespace schema: Can use multiple providers if you are authenticating from Portlets and Servlets
  • 20. AuthenticationDetailsSource <bean name=” portletAuthenticationDetailsSource” class=&quot;org.springframework.security.ui.portlet. PortletPreAuthenticatedAuthenticationDetailsSource&quot;> <property name=&quot;mappableRolesRetriever&quot;> <bean class=&quot;org.springframework.security. authoritymapping.SimpleMappableAttributesRetriever&quot;> <property name=&quot;mappableAttributes&quot;> <list> <value>ADMIN</value> </list> </property> </bean> </property> </bean> Can be used to check isUserInRole(...) to get list of Portal Roles into the Authentication Request: Only needed if we are using Portal Roles for our security decisions
  • 21. AuthenticationProvider <bean id=&quot;portletAuthenticationProvider&quot; class=&quot;org.springframework.security.providers.preauth. PreAuthenticatedAuthenticationProvider&quot;> <sec:custom-authentication-provider /> <property name=&quot;preAuthenticatedUserDetailsService&quot; ref=&quot; preAuthenticatedUserDetailsService &quot; /> </bean> PreAuthenticatedAuthenticationProvider processes pre-authenticated authentication request (from PortletProcessingInterceptor ) A valid PreAuthenticatedAuthenticationToken with non-null principal & credentials will succeed
  • 22. UserDetailsService <bean name=&quot;preAuthenticatedUserDetailsService&quot; class=&quot;org.springframework.security.providers.preauth. PreAuthenticatedGrantedAuthoritiesUserDetailsService&quot; /> Bean that knows how to populate user details (including GrantedAuthorities ) for the authenticated user PreAuthenticatedGrantedAuthoritiesUserDetailsService will use purely data contained in the PreAuthenticatedAuthenticationToken Can also use any other UserDetailsService that can populate UserDetails by username, such as JdbcUserDetailsManager or LdapUserDetailsManager
  • 23. PortletSessionContextIntegrationInterceptor <bean id=&quot;portletSessionContextIntegrationInterceptor&quot; class=&quot;org.springframework.security.context. PortletSessionContextIntegrationInterceptor&quot; /> Interceptor that retrieves/stores the contents of the SecurityContextHolder in the active PortletSession Without this, every request would trigger a full authentication cycle Default is to use APPLICATION_SCOPE Portlet equivalent of HttpSessionContextIntegrationFilter, used for traditional servlet web applications
  • 24. Using The Two Interceptors <bean id=&quot;portletModeHandlerMapping&quot; class=&quot;org.springframework.web.portlet.handler. PortletModeHandlerMapping&quot;> <property name=&quot;interceptors&quot;> <list> <ref bean=&quot; portletSessionContextIntegrationInterceptor &quot;/> <ref bean=&quot; portletProcessingInterceptor &quot;/> </list> </property> <property name=&quot;portletModeMap&quot;> <map> <entry key=&quot;view&quot;><ref bean=&quot;viewController&quot;/></entry> <entry key=&quot;edit&quot;><ref bean=&quot;editController&quot;/></entry> <entry key=&quot;help&quot;><ref bean=&quot;helpController&quot;/></entry> </map> </property> </bean> Add them to our Portlet's HandlerMapping : Warning! This ordering is critical – they will not work correctly if they are reversed!
  • 25. Applying Portlet Security To The Rendering Layer Customizing our markup based on security information
  • 26. Spring Security JSP TagLib <%@ taglib prefix=&quot;sec&quot; uri=&quot;http://www.springframework.org/security/tags&quot; %> <p>Username: <sec:authentication property=&quot;principal.username&quot;/></p> < sec :authorize ifAllGranted=&quot;ROLE_USER&quot;> <p>You are an authorized user of this system.</p> </ sec :authorize> <sec:authorize ifAllGranted=&quot;ROLE_ADMINISTRATOR&quot;> <p>You are an administrator of this system.</p> </sec:authorize> Allows us to access authentication information and to check authorizations Useful for showing/hiding information or navigation controls based on security info Warning: Don't rely on this to restrict access to areas of the application. Just because navigation doesn't appear in the markup doesn't mean a clever hacker can't generate a GET/POST that will still get there.
  • 27. Applying Portlet Security To The Dispatch Layer Controlling where users can go in the application
  • 28. Secure Portlet Request Dispatching Portlet Requests don't have a path structure, so we can't use the path-based patterns of FilterSecurityInterceptor to control access Something standard may be added in the future – perhaps a ConfigAttributeDefinition for various aspects of Portlet Requests that we can use as an ObjectDefinitionSource
  • 29. Using a HandlerInterceptor Best practice in Spring 2.0 is to build a custom HandlerInterceptor for your Portlet Compare contents of SecurityContextHolder. getContext(). getAuthentication() with Portlet Mode, Window State, Render Parameters – whatever you want to use to determine permission Throw a PortletSecurityException if access is not permitted, otherwise allow processing to proceed
  • 30. Using Annotations If using Spring 2.5 Annotation-based Dispatching, use Security Annotations as well ApplicationContext entry: Annotated method: <sec:global-method-security secured-annotations=&quot;enabled&quot; /> import org.springframework.security.annotation.Secured; ... @Secured({&quot;ROLE_ADMIN&quot;}) @RequestMapping(params=&quot;action=view&quot;) public String deleteItems(RequestParam(&quot;item&quot;) int itemId) { ...
  • 31. Applying Portlet Security To The Service Layer Making sure Services are invoked by only by user with proper permissions
  • 32. AccessDecisionManager <bean id=&quot;accessDecisionManager&quot; class=&quot;org.springframework.security.vote. AffirmativeBased&quot;> <property name=&quot;decisionVoters&quot;> <list> <bean class=&quot;org.springframework.security. vote.RoleVoter&quot; /> <bean class=&quot;org.springframework.security. vote.AuthenticatedVoter&quot; /> </list> </property> </bean> Standard Spring Security bean for making decisions about access to resources
  • 33. MethodSecurityInterceptor <bean id=&quot;myService&quot; class=&quot;sample.service.MyService&quot;> <sec:intercept-methods access-decision-manager-ref=&quot;accessDecisionManager&quot;> <sec:protect method=&quot;sample.service.MyService.*&quot; access=&quot;IS_AUTHENTICATED_FULLY&quot; /> <sec:protect method=&quot;sample.service.MyService.add*&quot; access=&quot;ROLE_ADMINISTRATOR&quot; /> <sec:protect method=&quot;sample.service.MyService.del*&quot; access=&quot;ROLE_ADMINISTRATOR&quot; /> <sec:protect method=&quot;sample.service.MyService.save*&quot; access=&quot;ROLE_ADMINISTRATOR&quot; /> </sec:intercept-methods> </bean>
  • 34. Applying Portlet Security To Servlets Using the whole web/portlet application as one secure bundle
  • 35. Bridging The Gap We can reuse the Portlet SecurityContext in getting resources from Servlets in the same web application Useful for securing: AJAX Calls Dynamic Images PDF Reports Need to get Portlets and Servlets to share session data to do this
  • 36. Portlets & Servlets Sharing Session Possible according to JSR 168 (PLT 15.4) Must be in the same webapp Portlet must use APPLICATION_SCOPE Sometime tricky in practice Portlet requests go thru Portal webapp URL Servlet requests go thru Portlet webapp URL Session tracking via JSESSIONID Cookie usually uses URL path to webapp – not shared! Tomcat 5.5.4 + On <Connector> element set emptySessionPath=true
  • 37. Apply Servlet Filter Chain In web.xml : <filter> <filter-name>securityFilterChainProxy</filter-name> <filter-class>org.springframework.web.filter. DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>securityFilterChainProxy</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
  • 38. FilterChainProxy Since the portal handles authentication, you only need a few entries in this bean : <bean id=&quot;servletSecurityFilterChainProxy&quot; class=&quot;org.springframework.security.util. FilterChainProxy&quot;> <sec:filter-chain-map path-type=&quot;ant&quot;> <sec:filter-chain pattern=&quot;/**&quot; filters=&quot;httpSessionContextIntegrationFilter, exceptionTranslationFilter, filterSecurityInterceptor&quot; /> </sec:filter-chain-map> </bean>
  • 39. HttpSessionContextIntegrationFilter If session sharing is working properly, it will populate the SecurityContextHolder using the same SecurityContext as the Portlet side <bean id=&quot;httpSessionContextIntegrationFilter&quot; class=&quot;org.springframework.security.context. HttpSessionContextIntegrationFilter&quot; /> This will only work if PortletSessionContextIntegrationInterceptor is storing in the APPLICATION_SCOPE of the PortletSession (which is the default)
  • 40. ExceptionTranslationFilter Since we are relying on the Portal for authentication, then an Exception means that authentication has already failed PreAuthenticatedProcessingFilterEntryPoint returns SC_FORBIDDEN (HTTP 403 error) <bean id=&quot;exceptionTranslationFilter&quot; class=&quot;org.springframework.security.ui. ExceptionTranslationFilter&quot;> <property name=&quot;authenticationEntryPoint&quot;> <bean class=&quot;org.springframework.security.ui.preauth. PreAuthenticatedProcessingFilterEntryPoint&quot; /> </property> </bean>
  • 41. FilterSecurityInterceptor Secure resource URLs accordingly Use the same AuthenticationManager and AccessDecisionManager as in the portlet <bean id=&quot;filterSecurityInterceptor&quot; class=&quot;org.springframework.security.intercept.web. FilterSecurityInterceptor&quot;> <property name=&quot;authenticationManager&quot; ref=&quot;authenticationManager&quot; /> <property name=&quot;accessDecisionManager&quot; ref=&quot;accessDecisionManager&quot; /> <property name=&quot;objectDefinitionSource&quot;> <sec:filter-invocation-definition-source> <sec:intercept-url pattern=&quot;/resources/**&quot; access=&quot;IS_AUTHENTICATED_FULLY&quot; /> </sec:filter-invocation-definition-source> </property> </bean>
  • 42. Resources Places to go to actually use this stuff!
  • 43. Resources Spring Security 2.0 Website http://static.springframework.org/spring-security/site/ Sample Applications Small sample included in Spring Security distro Bigger sample on the Spring Portlet Wiki http://opensource.atlassian.com/confluence/spring/display/JSR168/
  • 44. Questions & Answers John A. Lewis Chief Software Architect Unicon, Inc. [email_address] www.unicon.net