Org.springframework.security.authentication.internalauthenticationserviceexception: Userdetailsservice Returned Null, Which Is An Interface Contract Violation

 Posted admin
Org.springframework.security.authentication.internalauthenticationserviceexception: Userdetailsservice Returned Null, Which Is An Interface Contract Violation Average ratng: 5,8/10 2862 reviews

A simple spring boot app has a custom UserDetailsService. The Spring Boot Controller is called by an AngularJS app, and authentication requests from the AngularJS app are made to the backend at the /user url pattern. But login requests are causing the controller logs to indicate that the controller is not finding the /user url pattern, and that the UserDetailsService is thus returning null. What specific changes need to be made to the code below in order to enable client requests for the backend /user url pattern to find the controller method and thus also correctly instantiate the custom UserDetailsService?

前言本文是对 Spring Security Core 4.0.4 Release 进行源码分析的系列文章之一; 本文主要描述 Spring Security 的核心 Services, 本文为笔者的原创作品,转载需注明出处; 类图为了便于理解,笔者画了如下的一张类图,涵盖了 Spring Security 相关的 Core Services;如下, 概述两大核心 Services Authe. Java Code Examples for org.springframework.security.authentication. 'UserDetailsService returned null, which is an interface contract violation'); return.

Userdetails Spring Security

The app is so simple that the controller is less than 200 lines of code.

The code for the controller is:

And the complete stack trace produced by the login request is:

CodeMedCodeMed
4,17149 gold badges151 silver badges268 bronze badges

1 Answer

The controller method is found. The problem is this code:

As you clearly can see in the log messages:

Returning null is not allowed in a UserDetailsService implementation.

So check if you have an entry in your database for the admin user and remove the return null and throw a UsernameNotFoundException instead.

dunniSpringdunni
30.6k5 gold badges77 silver badges86 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Spring Security Userdetailsservice Not Called

Not the answer you're looking for? Browse other questions tagged javaspringspring-mvcspring-securityspring-boot or ask your own question.

I have implemented a login process using spring-security default POST /login.I have added configuration to return a JWT token in the header if the response is successful.This is working fine but I now have to write unit tests for each of my Controller's and endpoints.I am having problems testing this with MockMVC and cannot find a way around it. Any help would be much appreciated!

Sims 4 mods adults. Note:I have tried https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4

Inmemoryauthentication Custom Userdetails

Login Unit test

Error im getting

Abstract Setup for tests

Security Class's

JWTAuthentication Filter

JWTAuthorizationFilter

WebConfig

TechyLuke123TechyLuke123
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.

Browse other questions tagged springunit-testingtestingspring-securitymocking or ask your own question.