spring-projects/spring-security · error · ApplicationContextException

No UserDetailsService registered.

Error message

No UserDetailsService registered.

What it means

UserDetailsServiceFactoryBean.getUserDetailsService() looks up UserDetailsService beans in the context (preferring CachingUserDetailsService). When none exist, it throws because remember-me/x509 auto-configuration has no user source to authenticate tokens against.

Source

Thrown at config/src/main/java/org/springframework/security/config/http/UserDetailsServiceFactoryBean.java:108

				throw new ApplicationContextException(
						"Bean '" + name + "' must be a UserDetailsService or an" + " AuthenticationUserDetailsService");
			}
		}
		return new UserDetailsByNameServiceWrapper(uds);
	}

	/**
	 * Obtains a user details service for use in RememberMeServices etc. Will return a
	 * caching version if available so should not be used for beans which need to separate
	 * the two.
	 */
	private UserDetailsService getUserDetailsService() {
		Map<String, ?> beans = getBeansOfType(CachingUserDetailsService.class);
		if (beans.isEmpty()) {
			beans = getBeansOfType(UserDetailsService.class);
		}
		if (beans.isEmpty()) {
			throw new ApplicationContextException("No UserDetailsService registered.");
		}
		if (beans.size() > 1) {
			throw new ApplicationContextException("More than one UserDetailsService registered. Please "
					+ "use a specific Id reference in <remember-me/> or <x509 /> elements.");
		}
		return (UserDetailsService) beans.values().toArray()[0];
	}

	@Override
	public void setApplicationContext(ApplicationContext beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}

	private Map<String, ?> getBeansOfType(Class<?> type) {
		Map<String, ?> beans = this.beanFactory.getBeansOfType(type);
		// Check ancestor bean factories if they exist and the current one has none of the
		// required type
		BeanFactory parent = this.beanFactory.getParentBeanFactory();

View on GitHub (pinned to 96852e8860)

Solutions

  1. Register a UserDetailsService bean (e.g. InMemoryUserDetailsManager, JdbcUserDetailsManager, or custom) in the context.
  2. If more than one is intended, add an explicit id via remember-me's service-ref or x509's service-ref.
  3. For custom authentication logic, expose a UserDetailsService so Spring Security can resolve it.

Example fix

// before
<http>
  <remember-me key="myKey"/>
</http>

// after
<bean id="uds" class="org.springframework.security.core.userdetails.memory.InMemoryUserDetailsManager">
  <property name="userProperties"><value>user=pass,ROLE_USER</value></property>
</bean>
<http>
  <remember-me key="myKey" service-ref="uds"/>
</http>
Defensive patterns

Strategy: validation

Validate before calling

if (ctx.getBeansOfType(UserDetailsService.class).isEmpty()) {
    throw new IllegalStateException("Register a UserDetailsService before enabling remember-me/x509");
}

Try / catch

try { buildContext(); }
catch (ApplicationContextException e) {
  if (e.getMessage().contains("No UserDetailsService")) registerDefaultUds();
}

Prevention

When it happens

Trigger: Using <remember-me/> or <x509/> without an id ref while the application context contains zero beans of type UserDetailsService or CachingUserDetailsService.

Common situations: In-memory user store defined only via AuthenticationManagerBuilder without exposing a UserDetailsService bean; all user details beans removed during refactor; custom AuthenticationProvider used without a UserDetailsService.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-security@96852e8860 (2026-09-10). Data as JSON: /api/errors/06ae8e141fd4d8c3. Report an issue: GitHub.