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 3794704
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:58:13+00:00 2026-05-19T12:58:13+00:00

I tried searching in Google, but I could not find any good examples where

  • 0

I tried searching in Google, but I could not find any good examples where a username and password are checked with a database for authentication purposes.

In further simple words, how can I create a simple login form using Spring and Hibernate where the credentials are checked with the database.

Update

Cam anyone come up with a simple example where I can see how the flow goes and how the input data is passed to hibernate?

  • 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-05-19T12:58:14+00:00Added an answer on May 19, 2026 at 12:58 pm

    At first you should define this file WEB-INF/spring/serurity-context.xml:

    <beans:beans xmlns="http://www.springframework.org/schema/security"
                 xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                                     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">
    
        <http auto-config="true" />
    
        <beans:bean id="myUserService" class="org.my.UserService" />
        <authentication-provider user-service-ref="myUserService" />
    
    </beans:beans>
    

    Now you should create org.my.UserService class and implement interface org.springframework.security.core.userdetails.UserDetailsService. This interface has one method:

    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, org.springframework.dao.DataAccessException
    

    And in this method you can use Hibernate in order to load user by userName. If user does not exists – just throw UsernameNotFoundException, otherwise return new intialized UserDetails instance (there you can provide a lot of stuff like user roles, account expiration date, etc…).

    Now comes web.xml:

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             version="2.5">
    
        <display-name>My Webapp</display-name>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/*-context.xml
            </param-value>
        </context-param>
    
        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    If you have any questions or something goes wrong, feel free to ask 🙂

    PS: So with UserDetailsService you don’t have to check password of whether user account is active, etc. You just provide spring-security information about user with provided userName and framework validates user itself. If you encode your passwords with MD5 for example, than you can use password-encoder like this:

    <beans:bean id="myUserService" class="org.my.UserService" />
    <authentication-provider user-service-ref="myUserService">
        <password-encoder hash="md5"/>
    </authentication-provider>
    

    Update

    Now we will dive more deeper in UserService – my (simplified) real world example.

    UserService class:

    import org.my_company.my_app.domain.User
    
    public class UserService implements UserDetailsService {
        private UserDao userDao;
    
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
            // load user
            User user = userDao.getUser(username);
    
            if (user != null) {
    
                // convert roles
                List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
                for (Privilege p : user.getPrivileges()) {
                    roles.add(new GrantedAuthorityImpl(p.getName()));
                }
    
                // initialize user
                SecurityUser securityUser = new SecurityUser(
                    user.getUsername(),
                    user.getLdapAuth() ? getLdapPassword(user.getUsername()) : user.getPassword(),
                    user.getStatus() != User.Status.NOT_COMMITED, user.getStatus() != User.Status.BLOCKED, true, true,
                    roles.toArray(new GrantedAuthority[0])
                );
    
                securityUser.setUser(user);
    
                return securityUser;
            } else {
                throw new UsernameNotFoundException("No user with username '" + username + "' found!");
            }
        }
    }
    

    Now SecurityUser:

    import org.my_company.my_app.domain.User
    
    public class SecurityUser extends org.springframework.security.core.userdetails.User {
    
        private User user;
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    
        public SecurityUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, GrantedAuthority[] authorities) throws IllegalArgumentException {
            super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        }
    }
    

    And finally UserDao:

    import org.my_company.my_app.domain.User
    
    public class UserDao extends HibernateDaoSupport {
    
        public User getUser(String username) {
            List users = getHibernateTemplate().find("from User where username = ?", username);
            return users == null || users.size() <= 0 ? null : (User) users.get(0);
        }
    }
    

    As you can see I used HibernateTemplate here.

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

Sidebar

Related Questions

I tried searching in google but could not find any useful answer, if(verifier.length() >
I tried searching but could not find an answer for this. I am trying
I tried searching here and Google, but couldn't find a solution to the following:
I have tried to find an answer to this, but could not find one
I have tried searching on Google and also read the documentation but no success.
Tried searching the site, but cannot find an answer to my problem: Lets say
I tried searching for this but couldn't find a similar question. I have a
I tried searching for information on this, but must not know the right terms
I have tried searching over the internet about this problem but not able to
I've tried searching for an answer to this problem, but I'm not entirely sure

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.