Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8285325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:26:33+00:00 2026-06-08T11:26:33+00:00

i am trying to include spring security in a web application. For this i

  • 0

i am trying to include spring security in a web application. For this i wrote my own UserDetailsService implementation which looks like this:

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("userRepositoryImpl")
public class UserRepositoryImpl implements UserDetailsService {

        @PersistenceContext
        private EntityManager entityManager;

        @Override
        @Transactional(readOnly = true)
        public User loadUserByUsername(String username)
                        throws UsernameNotFoundException {
                User user = entityManager.find(User.class, username);
                if (user == null)
                        throw new UsernameNotFoundException("Username not found: "
                                        + username);
                return user;
        }

}

My problem is that the entityManager always is null when it calls the loadUserByUserName method.
I tried some things suggested here in some similar answers, but nothing helped.

This is my current security-app-context.xml:

<http use-expressions="true" auto-config="true">
    <intercept-url pattern="/task/" access="permitAll" />
    <intercept-url pattern="/task/**" access="isAuthenticated()" />

    <!-- <intercept-url pattern="/**" access="denyAll" /> -->
    <form-login />
</http>

<beans:bean id="userRepositoryImpl" class="de.sveri.jeiwomisa.model.UserRepositoryImpl" autowire="byType">
</beans:bean>        
<beans:bean id="passwordEncoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder">
</beans:bean>
<authentication-manager>
    <authentication-provider user-service-ref="userRepositoryImpl">
        <password-encoder ref="passwordEncoder" />
    </authentication-provider>
</authentication-manager>

Any Ideas what might be wrong there?

Regards,
Sven

Edit: Trying a bit more i found out that i cannot use the entityManager in my generic DaoImpl during the login process.
But i can use it sending and receiving data in another process. This is the Dao:

@Transactional
public abstract class DaoImpl<T> implements Dao<T> {

    private Class<T> type;

    @PersistenceContext
    protected EntityManager em;

    public DaoImpl() {
            Type t = getClass().getGenericSuperclass();
            ParameterizedType pt = (ParameterizedType) t;
            type = (Class) pt.getActualTypeArguments()[0];
    }
...
}

I wonder why it is like this. Obviously i dont understand much of how spring works, so it would be great if someone could shed a light.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:26:34+00:00Added an answer on June 8, 2026 at 11:26 am

    Thanks to a nice guy from the spring team in the forums i got it working.
    I had to change the xml definition and add a tag so that my security-app-context.xml looks like this:

    <b:beans xmlns:b="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/security"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/security     http://www.springframework.org/schema/security/spring-security-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
        <context:annotation-config/>
    
    ....
    

    This is the link to the forum entry: http://forum.springsource.org/showthread.php?128626-EntityManager-null-when-implementing-UserDetailsService

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract urls from strings. So i've wrote soomething like this:
We are using various plugins in our grails application (like logging, spring security core,
I'm trying to do this: #pragma once #include <fstream> #include <string> static std::ofstream ErrorLog;
I'm trying include this barcode source code Stefanhafeneger - Barcode github.com to my project
I am trying to include <% @using System.Web.Script.Serialization%> but it is giving me error
I am trying to query mongodb using spring. We have a collection which holds
While trying to debug my openid implementation with Google, which kept returning Apache 406
I'm trying to implement a new security bean, which relies on some Base64 encoding.
I'm trying to use spring-test-mvc for testing a controller in my little application. Since
I am trying to use NamedParameterJdbTemplate in a Spring MVC application. The issue is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.