Class SecurityContextHolderAwareRequestWrapper
- java.lang.Object
-
- javax.servlet.ServletRequestWrapper
-
- javax.servlet.http.HttpServletRequestWrapper
-
- org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper
-
- All Implemented Interfaces:
javax.servlet.http.HttpServletRequest
,javax.servlet.ServletRequest
public class SecurityContextHolderAwareRequestWrapper extends javax.servlet.http.HttpServletRequestWrapper
A Spring Security-awareHttpServletRequestWrapper
, which uses theSecurityContext
-definedAuthentication
object to implement the servlet API security methods:getUserPrincipal()
isUserInRole(String)
HttpServletRequestWrapper.getRemoteUser()
.
- See Also:
SecurityContextHolderAwareRequestFilter
-
-
Constructor Summary
Constructors Constructor Description SecurityContextHolderAwareRequestWrapper(javax.servlet.http.HttpServletRequest request, java.lang.String rolePrefix)
Creates a new instance withAuthenticationTrustResolverImpl
.SecurityContextHolderAwareRequestWrapper(javax.servlet.http.HttpServletRequest request, AuthenticationTrustResolver trustResolver, java.lang.String rolePrefix)
Creates a new instance
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
getRemoteUser()
Returns the principal's name, as obtained from theSecurityContextHolder
.java.security.Principal
getUserPrincipal()
Returns theAuthentication
(which is a subclass ofPrincipal
), ornull
if unavailable.boolean
isUserInRole(java.lang.String role)
Simple searches for an exactly matchingGrantedAuthority.getAuthority()
.void
setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy)
Sets theSecurityContextHolderStrategy
to use.java.lang.String
toString()
-
Methods inherited from class javax.servlet.http.HttpServletRequestWrapper
authenticate, changeSessionId, getAuthType, getContextPath, getCookies, getDateHeader, getHeader, getHeaderNames, getHeaders, getHttpServletMapping, getIntHeader, getMethod, getPart, getParts, getPathInfo, getPathTranslated, getQueryString, getRequestedSessionId, getRequestURI, getRequestURL, getServletPath, getSession, getSession, getTrailerFields, isRequestedSessionIdFromCookie, isRequestedSessionIdFromUrl, isRequestedSessionIdFromURL, isRequestedSessionIdValid, isTrailerFieldsReady, login, logout, newPushBuilder, upgrade
-
Methods inherited from class javax.servlet.ServletRequestWrapper
getAsyncContext, getAttribute, getAttributeNames, getCharacterEncoding, getContentLength, getContentLengthLong, getContentType, getDispatcherType, getInputStream, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getParameter, getParameterMap, getParameterNames, getParameterValues, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequest, getRequestDispatcher, getScheme, getServerName, getServerPort, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, isWrapperFor, isWrapperFor, removeAttribute, setAttribute, setCharacterEncoding, setRequest, startAsync, startAsync
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface javax.servlet.ServletRequest
getAsyncContext, getAttribute, getAttributeNames, getCharacterEncoding, getContentLength, getContentLengthLong, getContentType, getDispatcherType, getInputStream, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getParameter, getParameterMap, getParameterNames, getParameterValues, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequestDispatcher, getScheme, getServerName, getServerPort, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, removeAttribute, setAttribute, setCharacterEncoding, startAsync, startAsync
-
-
-
-
Constructor Detail
-
SecurityContextHolderAwareRequestWrapper
public SecurityContextHolderAwareRequestWrapper(javax.servlet.http.HttpServletRequest request, java.lang.String rolePrefix)
Creates a new instance withAuthenticationTrustResolverImpl
.- Parameters:
request
-rolePrefix
-
-
SecurityContextHolderAwareRequestWrapper
public SecurityContextHolderAwareRequestWrapper(javax.servlet.http.HttpServletRequest request, AuthenticationTrustResolver trustResolver, java.lang.String rolePrefix)
Creates a new instance- Parameters:
request
- the originalHttpServletRequest
trustResolver
- theAuthenticationTrustResolver
to use. Cannot be null.rolePrefix
- The prefix to be added toisUserInRole(String)
or null if no prefix.
-
-
Method Detail
-
getRemoteUser
public java.lang.String getRemoteUser()
Returns the principal's name, as obtained from theSecurityContextHolder
. Properly handles bothString
-based andUserDetails
-based principals.- Specified by:
getRemoteUser
in interfacejavax.servlet.http.HttpServletRequest
- Overrides:
getRemoteUser
in classjavax.servlet.http.HttpServletRequestWrapper
- Returns:
- the username or
null
if unavailable
-
getUserPrincipal
public java.security.Principal getUserPrincipal()
Returns theAuthentication
(which is a subclass ofPrincipal
), ornull
if unavailable.- Specified by:
getUserPrincipal
in interfacejavax.servlet.http.HttpServletRequest
- Overrides:
getUserPrincipal
in classjavax.servlet.http.HttpServletRequestWrapper
- Returns:
- the
Authentication
, ornull
-
isUserInRole
public boolean isUserInRole(java.lang.String role)
Simple searches for an exactly matchingGrantedAuthority.getAuthority()
.Will always return
false
if theSecurityContextHolder
contains anAuthentication
withnull
principal
and/orGrantedAuthority[]
objects.- Specified by:
isUserInRole
in interfacejavax.servlet.http.HttpServletRequest
- Overrides:
isUserInRole
in classjavax.servlet.http.HttpServletRequestWrapper
- Parameters:
role
- theGrantedAuthority
String
representation to check for- Returns:
true
if an exact (case sensitive) matching granted authority is located,false
otherwise
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
setSecurityContextHolderStrategy
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy)
Sets theSecurityContextHolderStrategy
to use. The default action is to use theSecurityContextHolderStrategy
stored inSecurityContextHolder
.- Since:
- 5.8
-
-