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

  • Home
  • SEARCH
  • 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 8223755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:49:32+00:00 2026-06-07T14:49:32+00:00

I have got stuck at creating usefull form login in SpringMVC using Spring Security.

  • 0

I have got stuck at creating usefull form login in SpringMVC using Spring Security. I’m quite newbie in this and also Hibernate. I would like to create simple form login which could provide access to my web application.

I have created my project using SpringSource Tool Suite and selecting Spring Template Project. It uses Maven and I have also generated by Hibernate classes with annotations and hibernate.cfg.xml. In my database (HSQLDB) I have three tables: users, roles and users_roles. The third one contains user_id and role_id, so it stores information about users’ roles. I have generated class by Hibernate successful.

I have started writing my class implementing UserDetailsService. But I don’t know how to do this properly. In spring-security.xml I have defined bean like this:

<bean id="userDetailsService" class="hutter.pl.services.HutterUserDetailsService" />

I would like to use hashing by sha-256 with saltSource.

<bean class="org.springframework.security.authentication.dao.ReflectionSaltSource" id="saltSource">
    <property name="userPropertyToUse" value="username"/>
</bean>

<security:authentication-manager>   
    <security:authentication-provider user-service-ref="userDetailsService">
        <security:password-encoder hash="sha-256">
            <security:salt-source ref="saltSource" />
        </security:password-encoder> 
    </security:authentication-provider>
</security:authentication-manager>

Should I have used this solution: https://stackoverflow.com/a/1654488/845220 ? Hibernate have generted classes like: RolesHome, Roles, Users, UsersHome, UsersRoles, UsersRolesHome. But i really don’t know how to use these Hibernates classes to authorize users.

@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {   
        UsersHome usersHome = new UsersHome();
       //Users user = ...       
       //...            
       return null;     
    }   
}

Could you give me some hints?

EDIT:
I have tried to add method public Users findByLogin(String login) to the UsersHome class.

   public Users findByLogin(String login) {
    log.debug("getting Users instance with login: " + login);
    try {
        Users instance = entityManager.find(Users.class, login);
        log.debug("get successful");
        return instance;
    } catch (RuntimeException re) {
        log.error("get failed", re);
        throw re;
    }
}

And body of my UserDetailsService looks like:

UsersHome usersHome = new UsersHome();
Users user = usersHome.findByLogin(username);

But I have got excpetion:

 ERROR: my.package.dao.UsersHome - get failed
 java.lang.NullPointerException
at my.package.dao.UsersHome.findByLogin(UsersHome.java:72)
at my.package.services.HutterUserDetailsService.loadUserByUsername(MyUserDetailsService.java:19)
  • 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-07T14:49:34+00:00Added an answer on June 7, 2026 at 2:49 pm

    I think you don’t need to implement an UserService yourself. You can use a jdbc-user-service with a datasource:

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver" />
      <property name="url" value="jdbc:mysql://localhost:3306/mydb" />
      <property name="username" value="root" />
      <property name="password" value="password" />
    </bean>
    
    <authentication-manager>
      <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"
          users-by-username-query="select username,password, enabled from users where username=?"
          authorities-by-username-query="select u.username, ur.authority from users u, user_roles ur  where u.user_id = ur.user_id and u.username =?" 
        />
      </authentication-provider>
    </authentication-manager>
    

    With the properties users-by-username-query and users-by-username-query you can define the queries spring security should use to receive the users and authorities from the datasource.

    Implementing your own UserService is necessary if

    • you want to return customized UserDetails objects (which you can access later via the SecurityContextHolder)
    • the receival of the User objects and authorities is too complex and/or cannot be defined with easy queries at the jdbc-user-service

    A possible implementation of UserDetailsService could look like this:

    @Service("userDetailsService")
    public class MyUserDetailsService implements UserDetailsService {
    
      @PersistenceContext
      private EntityManager entityManager;
    
      @Transactional(readOnly = true)
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {   
    
        // this works only if username is the primary key of user
        // if thats not the case you have to create a query object to receive the user by username
        User user = entityManager.find(User.class, username); 
    
        List<GrantedAuthority> roles = .... // get roles for user, depends on your table structure
    
        if (user == null) {
          // user not found
          throw new UsernameNotFoundException();
        }
        return new MyUserDetails(user, roles);
      }
    
      private static class MyUserDetails implements UserDetails {
        private User user;
        private List<Role> roles;
    
        public MyUserDetails(Usere user, List<GrantedAuthority> roles) {
          this.user = user;
          this.roles = roles;
        }
    
        public Collection<GrantedAuthority> getAuthorities() {
          return roles;
        }
    
        public String getPassword() {
          return user.getPassword();
        }
    
        public String getUsername() {
          return user.getUsername();
        }
    
        // return true for the missing boolean methods..
      }
    }
    

    (syntax is unchecked)

    For first testing it can help to disable the password encoder and store the password unencrypted in the database. This avoids the problem that your authentication does not work because of wrongly configured PasswordEncoders. Once you have your userservice running you can then add a PasswordEncoder again and store the hashed password in the database.

    Hope it helps 🙂

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

Sidebar

Related Questions

I got stuck with this scenario. Let me explain as follows. I have a
I am currently creating an order form. However, I have been stuck at the
I am currently creating an order form. However, I have been stuck at the
I am working on a problem and got stuck at a wall I have
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
I have got method which is using Assembly.LoadFrom(...) statement and returns the supported cultures
I am developing a facebook app and I seem to have got stuck on
I'm relative new to android programming and layouts, and got stuck on creating a
I am currently working on creating a vertical scroller and have got so far
I am trying to make a mysql table and have got stuck with making

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.