Class SecurityRealm
- All Implemented Interfaces:
- ExtensionPoint,- Describable<SecurityRealm>
- Direct Known Subclasses:
- AbstractPasswordBasedSecurityRealm,- LegacySecurityRealm
 If additional views/URLs need to be exposed,
 an active SecurityRealm is bound to CONTEXT_ROOT/securityRealm/
 through Jenkins.getSecurityRealm(), so you can define additional pages and
 operations on your SecurityRealm.
 
How do I implement this class?
For compatibility reasons, there are two somewhat different ways to implement a custom SecurityRealm.
 One is to override the createSecurityComponents() and create key Spring Security components
 that control the authentication process.
 The default createFilter(FilterConfig) implementation then assembles them
 into a chain of Filters. All the incoming requests to Hudson go through this filter chain,
 and when the filter chain is done, SecurityContext.getAuthentication() would tell us
 who the current user is.
 
 If your SecurityRealm needs to touch the default Filter chain configuration
 (e.g., adding new ones), then you can also override createFilter(FilterConfig) to do so.
 
 This model is expected to fit most SecurityRealm implementations.
 
 The other way of doing this is to ignore createSecurityComponents() completely (by returning
 SecurityRealm.SecurityComponents created by the default constructor) and just concentrate on createFilter(FilterConfig).
 As long as the resulting filter chain properly sets up Authentication object at the end of the processing,
 Jenkins doesn't really need you to fit the standard Spring Security models like AuthenticationManager and
 UserDetailsService.
 
This model is for those "weird" implementations.
Views
- loginLink.jelly
- 
 This view renders the login link on the top right corner of every page, when the user
 is anonymous. For SecurityRealms that support user sign-up, this is a good place to show a "sign up" link. SeeHudsonPrivateSecurityRealmimplementation for an example of this.
- config.jelly
- This view is used to render the configuration page in the system config screen.
- Since:
- 1.160
- Author:
- Kohsuke Kawaguchi
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic final classJust a tuple so that we can create various inter-related security related objects and return them all at once.Nested classes/interfaces inherited from interface hudson.ExtensionPointExtensionPoint.LegacyInstancesAreScopedToHudson
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final GrantedAuthorityDeprecated.static final org.springframework.security.core.GrantedAuthorityGrantedAuthoritythat represents the built-in "authenticated" role, which is granted to anyone non-anonymous.static final DescriptorList<SecurityRealm> static final SecurityRealmSingleton constant that represents "no authentication."
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionall()Returns all the registeredSecurityRealmdescriptors.booleanReturns true if thisSecurityRealmallows online sign-up.booleanReturns true if thisSecurityRealmsupports explicit logout operation.org.kohsuke.stapler.HttpResponseStarts the user registration process for a new user that has the given verified identity.protected final List<jakarta.servlet.Filter> createCliAuthenticator(CLICommand command) Deprecated.No longer used.jakarta.servlet.FiltercreateFilter(jakarta.servlet.FilterConfig filterConfig) CreatesFilterthat all the incoming HTTP requests will go through for authentication.javax.servlet.FiltercreateFilter(javax.servlet.FilterConfig filterConfig) Deprecated.abstract SecurityRealm.SecurityComponentsCreates fully-configuredAuthenticationManagerthat performs authentication against the user realm.final voiddoCaptcha(org.kohsuke.stapler.StaplerRequest2 req, org.kohsuke.stapler.StaplerResponse2 rsp) Generates a captcha image.voiddoLogout(org.kohsuke.stapler.StaplerRequest2 req, org.kohsuke.stapler.StaplerResponse2 rsp) Handles the logout processing.voiddoLogout(org.kohsuke.stapler.StaplerRequest req, org.kohsuke.stapler.StaplerResponse rsp) Deprecated.Returns the URL to submit a form for the authentication.Gets the descriptor for this instance.static StringgetFrom()Perform a calculation where we should go back after successful loginReturns theIdStrategythat should be used for turningGroupDetails.getName()into an ID.Gets the target URL of the "login" link.protected StringgetPostLogOutUrl(org.kohsuke.stapler.StaplerRequest req, Authentication auth) protected StringgetPostLogOutUrl2(org.kohsuke.stapler.StaplerRequest2 req, org.springframework.security.core.Authentication auth) Controls where the user is sent to after a logout.protected StringgetPostLogOutUrl2(org.kohsuke.stapler.StaplerRequest req, org.springframework.security.core.Authentication auth) Deprecated.Use this function to get the security components, without necessarily recreating them.Returns theIdStrategythat should be used for turningUserDetails.getUsername()into an ID.loadGroupByGroupname(String groupname) Deprecated.loadGroupByGroupname(String groupname, boolean fetchMembers) Deprecated.loadGroupByGroupname2(String groupname, boolean fetchMembers) If thisSecurityRealmsupports a look up ofGroupDetailsby their names, override this method to provide the look up.loadUserByUsername(String username) Deprecated.org.springframework.security.core.userdetails.UserDetailsloadUserByUsername2(String username) Shortcut forUserDetailsService.loadUserByUsername(String).voidsetCaptchaSupport(CaptchaSupport captchaSupport) protected final booleanvalidateCaptcha(String text) Validates the captcha.
- 
Field Details- 
NO_AUTHENTICATIONSingleton constant that represents "no authentication."
- 
LISTAll registeredSecurityRealmimplementations.
- 
AUTHENTICATED_AUTHORITY2public static final org.springframework.security.core.GrantedAuthority AUTHENTICATED_AUTHORITY2GrantedAuthoritythat represents the built-in "authenticated" role, which is granted to anyone non-anonymous.- Since:
- 2.266
 
- 
AUTHENTICATED_AUTHORITYDeprecated.
 
- 
- 
Constructor Details- 
SecurityRealmpublic SecurityRealm()
 
- 
- 
Method Details- 
createSecurityComponentsCreates fully-configuredAuthenticationManagerthat performs authentication against the user realm. The implementation hides how such authentication manager is configured.AuthenticationManagerinstantiation often depends on the user-specified parameters (for example, if the authentication is based on LDAP, the user needs to specify the host name of the LDAP server.) Such configuration is expected to be presented to the user viaconfig.jellyand then captured as instance variables inside theSecurityRealmimplementation.Your SecurityRealmmay also wants to alterFilterset up by overridingcreateFilter(FilterConfig).
- 
getUserIdStrategyReturns theIdStrategythat should be used for turningUserDetails.getUsername()into an ID. Mostly this should beIdStrategy.CaseInsensitivebut there may be occasions when eitherIdStrategy.CaseSensitiveorIdStrategy.CaseSensitiveEmailAddressare the correct approach.- Returns:
- the IdStrategythat should be used for turningUserDetails.getUsername()into an ID.
- Since:
- 1.566
 
- 
getGroupIdStrategyReturns theIdStrategythat should be used for turningGroupDetails.getName()into an ID. Note: Mostly this should be the same asgetUserIdStrategy()but some security realms may have legitimate reasons for a different strategy.- Returns:
- the IdStrategythat should be used for turningGroupDetails.getName()into an ID.
- Since:
- 1.566
 
- 
createCliAuthenticatorDeprecated.No longer used.
- 
getDescriptorGets the descriptor for this instance.Descriptoris a singleton for every concreteDescribableimplementation, so ifa.getClass() == b.getClass()then by defaulta.getDescriptor() == b.getDescriptor()as well. (In rare cases a single implementation class may be used for instances with distinct descriptors.)By default looks for a nested class (conventionally named DescriptorImpl) implementingDescriptorand marked withExtension.SecurityRealmis a singleton resource in Hudson, and therefore it's always configured throughconfig.jellyand never withglobal.jelly.- Specified by:
- getDescriptorin interface- Describable<SecurityRealm>
 
- 
getAuthenticationGatewayUrlReturns the URL to submit a form for the authentication. There's no need to override this, except forLegacySecurityRealm.- See Also:
 
- 
getLoginUrlGets the target URL of the "login" link. There's no need to override this, except forLegacySecurityRealm. On legacy implementation this should point tologinEntry, which is protected byweb.xml, so that the user can be eventually authenticated by the container.Path is relative from the context root of the Hudson application. The URL returned by this method will get the "from" query parameter indicating the page that the user was at. 
- 
canLogOutpublic boolean canLogOut()Returns true if thisSecurityRealmsupports explicit logout operation.If the method returns false, "logout" link will not be displayed. This is useful when authentication doesn't require an explicit login activity (such as NTLM authentication or Kerberos authentication, where Hudson has no ability to log off the current user.) By default, this method returns true. - Since:
- 1.307
 
- 
getPostLogOutUrl2protected String getPostLogOutUrl2(org.kohsuke.stapler.StaplerRequest2 req, org.springframework.security.core.Authentication auth) Controls where the user is sent to after a logout. By default, it's the top page of Hudson, but you can return arbitrary URL.- Parameters:
- req-- StaplerRequest2that represents the current request. Primarily so that you can get the context path. By the time this method is called, the session is already invalidated. Never null.
- auth- The- Authenticationobject that represents the user that was logging in. This parameter allows you to redirect people to different pages depending on who they are.
- Returns:
- never null.
- Since:
- 2.475
- See Also:
 
- 
getPostLogOutUrl2@Deprecated protected String getPostLogOutUrl2(org.kohsuke.stapler.StaplerRequest req, org.springframework.security.core.Authentication auth) Deprecated.- Since:
- 2.266
 
- 
getPostLogOutUrl@Deprecated protected String getPostLogOutUrl(org.kohsuke.stapler.StaplerRequest req, Authentication auth) Deprecated.- Since:
- 1.314
 
- 
getCaptchaSupport
- 
setCaptchaSupport
- 
getCaptchaSupportDescriptors
- 
doLogoutpublic void doLogout(org.kohsuke.stapler.StaplerRequest2 req, org.kohsuke.stapler.StaplerResponse2 rsp) throws IOException, jakarta.servlet.ServletException Handles the logout processing.The default implementation erases the session and do a few other clean up, then redirect the user to the URL specified by getPostLogOutUrl2(StaplerRequest2, Authentication).- Throws:
- IOException
- jakarta.servlet.ServletException
- Since:
- 2.475
 
- 
doLogout@Deprecated @StaplerNotDispatchable public void doLogout(org.kohsuke.stapler.StaplerRequest req, org.kohsuke.stapler.StaplerResponse rsp) throws IOException, javax.servlet.ServletException Deprecated.- Throws:
- IOException
- javax.servlet.ServletException
- Since:
- 1.314
 
- 
allowsSignuppublic boolean allowsSignup()Returns true if thisSecurityRealmallows online sign-up. This creates a hyperlink that redirects users toCONTEXT_ROOT/signUp, which will be served by thesignup.jellyview of this class.If the implementation needs to redirect the user to a different URL for signing up, use the following jelly script as signup.jelly<xmp> <st:redirect url="http://www.sun.com/" xmlns:st="jelly:stapler"/> </xmp>
- 
loadUserByUsername2public org.springframework.security.core.userdetails.UserDetails loadUserByUsername2(String username) throws org.springframework.security.core.userdetails.UsernameNotFoundException Shortcut forUserDetailsService.loadUserByUsername(String).- Returns:
- never null.
- Throws:
- UserMayOrMayNotExistException2- If the security realm cannot even tell if the user exists or not.
- org.springframework.security.core.userdetails.UsernameNotFoundException
- Since:
- 2.266
 
- 
loadUserByUsername@Deprecated public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException Deprecated.
- 
loadGroupByGroupname2public GroupDetails loadGroupByGroupname2(String groupname, boolean fetchMembers) throws org.springframework.security.core.userdetails.UsernameNotFoundException If thisSecurityRealmsupports a look up ofGroupDetailsby their names, override this method to provide the look up.This information, when available, can be used by AuthorizationStrategys to improve the UI and error diagnostics for the user.- Parameters:
- groupname- the name of the group to fetch
- fetchMembers- if- truethen try and fetch the members of the group if it exists. Trying does not imply that the members will be fetched and- GroupDetails.getMembers()may still return- null
- Throws:
- UserMayOrMayNotExistException2- if no conclusive result could be determined regarding the group existence.
- org.springframework.security.core.userdetails.UsernameNotFoundException- if the group does not exist.
- Since:
- 2.266
 
- 
loadGroupByGroupname@Deprecated public GroupDetails loadGroupByGroupname(String groupname) throws UsernameNotFoundException, DataAccessException Deprecated.
- 
loadGroupByGroupname@Deprecated public GroupDetails loadGroupByGroupname(String groupname, boolean fetchMembers) throws UsernameNotFoundException, DataAccessException Deprecated.- Throws:
- UsernameNotFoundException
- DataAccessException
- Since:
- 1.549
 
- 
commenceSignuppublic org.kohsuke.stapler.HttpResponse commenceSignup(FederatedLoginService.FederatedIdentity identity) Starts the user registration process for a new user that has the given verified identity.If the user logs in through a FederatedLoginService, verified that the current user owns an identity, but no existing user account has claimed that identity, then this method is invoked.The expected behaviour is to confirm that the user would like to create a new account, and associate this federated identity to the newly created account (via FederatedLoginService.FederatedIdentity.addToCurrentUser().- Throws:
- UnsupportedOperationException- If this implementation doesn't support the signup through this mechanism. This is the default implementation.
- Since:
- 1.394
 
- 
doCaptchapublic final void doCaptcha(org.kohsuke.stapler.StaplerRequest2 req, org.kohsuke.stapler.StaplerResponse2 rsp) throws IOException Generates a captcha image.- Throws:
- IOException
 
- 
validateCaptchaValidates the captcha.
- 
getSecurityComponentsUse this function to get the security components, without necessarily recreating them.
- 
createFilterpublic jakarta.servlet.Filter createFilter(jakarta.servlet.FilterConfig filterConfig) CreatesFilterthat all the incoming HTTP requests will go through for authentication.The default implementation uses getSecurityComponents()and builds a standard filter chain. But subclasses can override this to completely change the filter sequence.For other plugins that want to contribute Filter, seePluginServletFilter.- Since:
- 2.475
 
- 
createFilterDeprecated.- Since:
- 1.271
 
- 
commonFilters
- 
getFromPerform a calculation where we should go back after successful login- Returns:
- Encoded URI where we should go back after successful login or "/" if no way back or an issue occurred
- Since:
- 2.4
 
- 
allReturns all the registeredSecurityRealmdescriptors.
 
- 
AUTHENTICATED_AUTHORITY2