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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:36:06+00:00 2026-06-07T20:36:06+00:00

I haven’t been able to figure out what I am doing wrong here. I

  • 0

I haven’t been able to figure out what I am doing wrong here. I am trying to access the page /user/test.jsp but I am having an error 403 access denied error.

I guess the problem is coming from the ManyToMany annotations in the UserEntity class. I have tried everything I could but am still not successful in solving this.

After further research, it appears that the user_security_role are not loaded from the DB.

I know it is the case since the following portion of code in the buildUserFromUserEntity in the Assembler class return an empty SecurityRoleCollection:

for (SecurityRoleEntity role : userEntity.getSecurityRoleCollection()) {
    authorities.add(new GrantedAuthorityImpl(role.getName()));
}

Here are the SQL I used for the table (from the tutorial):

CREATE TABLE IF NOT EXISTS security_role (

    `id` INT(11) NOT NULL AUTO_INCREMENT ,

    `name` VARCHAR(50) NULL DEFAULT NULL ,

    PRIMARY KEY (`id`) )

    ENGINE = InnoDB

    AUTO_INCREMENT = 4

    DEFAULT CHARACTER SET = latin1;


CREATE TABLE IF NOT EXISTS user (

    `id` INT(11) NOT NULL AUTO_INCREMENT ,

    `first_name` VARCHAR(45) NULL DEFAULT NULL ,

    `family_name` VARCHAR(45) NULL DEFAULT NULL ,

    `dob` DATE NULL DEFAULT NULL ,

    `password` VARCHAR(45) NOT NULL ,

    `username` VARCHAR(45) NOT NULL ,

    `confirm_password` VARCHAR(45) NOT NULL ,

    `active` TINYINT(1) NOT NULL ,

    PRIMARY KEY (`id`) ,

    UNIQUE INDEX `username` (`username` ASC) )

    ENGINE = InnoDB

    AUTO_INCREMENT = 9

    DEFAULT CHARACTER SET = latin1;


CREATE TABLE IF NOT EXISTS user_security_role (

    `user_id` INT(11) NOT NULL ,

    `security_role_id` INT(11) NOT NULL ,

    PRIMARY KEY (`user_id`, `security_role_id`) ,

    INDEX `security_role_id` (`security_role_id` ASC) ,

    CONSTRAINT `user_security_role_ibfk_1`

    FOREIGN KEY (`user_id` )

    REFERENCES `user` (`id` ),

    CONSTRAINT `user_security_role_ibfk_2`

    FOREIGN KEY (`security_role_id` )

    REFERENCES `security_role` (`id` ))

    ENGINE = InnoDB

    DEFAULT CHARACTER SET = latin1;

Here is the tricky part I can’t get to work:
The annotations configured for the private Set securityRoleCollection; attribute do not load the data from the DB, I can’t figure out what I am doing wrong here.

public class UserEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "family_name")
    private String familyName;

    @Column(name = "dob")
    @Temporal(TemporalType.DATE)
    private Date dob;

    @Basic(optional = false)
    @Column(name = "password")
    private String password;

    @Basic(optional = false)
    @Column(name = "username")
    private String username;

    @Basic(optional = false)
    @Column(name = "confirm_password")
    private String confirmPassword;

    @Basic(optional = false)
    @Column(name = "active")
    private boolean active;

    @JoinTable(name = "user_security_role", joinColumns = {

    @JoinColumn(name = "user_id", referencedColumnName = "id") }, inverseJoinColumns = {

    @JoinColumn(name = "security_role_id", referencedColumnName = "id") })
    @ManyToMany
    private Set<SecurityRoleEntity> securityRoleCollection;

    public UserEntity() {

    }

Here is my UserEntityDAOImpl class:

public class UserEntityDAOImpl implements UserEntityDAO {

    public UserEntity findByName(String username) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        UserEntity user = null;
        try {
            transaction = session.beginTransaction();
            user = (UserEntity)session.createQuery("select u from UserEntity u where u.username = '"
                    + username + "'").uniqueResult();

            transaction.commit();
        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
        return user;
    }

Here is the SecurityRoleEntity class:

@Entity
@Table(name = "security_role", catalog = "userauth", schema = "")
@NamedQueries({

        @NamedQuery(name = "SecurityRoleEntity.findAll", query = "SELECT s FROM SecurityRoleEntity s"),

        @NamedQuery(name = "SecurityRoleEntity.findById", query = "SELECT s FROM SecurityRoleEntity s WHERE s.id = :id"),

        @NamedQuery(name = "SecurityRoleEntity.findByName", query = "SELECT s FROM SecurityRoleEntity s WHERE s.name = :name") })
public class SecurityRoleEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @ManyToMany(mappedBy = "securityRoleCollection", fetch = FetchType.EAGER)
    private List<UserEntity> userCollection;

    public SecurityRoleEntity() {

    }

        getters and setters...

Here is the assembler class:

@Service("assembler")
public class Assembler {

    @Transactional(readOnly = true)
    User buildUserFromUserEntity(UserEntity userEntity) {

        String username = userEntity.getUsername();
        String password = userEntity.getPassword();
        boolean enabled = userEntity.getActive();
        boolean accountNonExpired = userEntity.getActive();
        boolean credentialsNonExpired = userEntity.getActive();
        boolean accountNonLocked = userEntity.getActive();

        Collection<GrantedAuthorityImpl> authorities = new ArrayList<GrantedAuthorityImpl>();

        for (SecurityRoleEntity role : userEntity.getSecurityRoleCollection()) {
            authorities.add(new GrantedAuthorityImpl(role.getName()));
        }

        User user = new User(username, password, enabled,
        accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);

        return user;

    }

}

Here is the content of my spring-security.xml:

<beans:bean id="userDetailsService" class="service.UserDetailsServiceImpl">
</beans:bean>

<beans:bean id="assembler" class="service.Assembler">
</beans:bean>

<!-- <context:component-scan base-package="org.intan.pedigree" /> -->

<http auto-config='true'>
    <intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
    <intercept-url pattern="/user/**" access="ROLE_User" />
    <!-- <security:intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" 
        /> -->
</http>

<beans:bean id="daoAuthenticationProvider"
    class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <beans:property name="userDetailsService" ref="userDetailsService" />
</beans:bean>

<beans:bean id="authenticationManager"
    class="org.springframework.security.authentication.ProviderManager">
    <beans:property name="providers">
        <beans:list>
            <beans:ref local="daoAuthenticationProvider" />
        </beans:list>
    </beans:property>
</beans:bean>

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService">
        <password-encoder hash="plaintext" />
    </authentication-provider>
</authentication-manager>

Here is the UserDetailsServiceImpl:

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

    @Autowired
    private UserEntityDAO dao;

    @Autowired
    private Assembler assembler;

    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String username)

    throws UsernameNotFoundException, DataAccessException {

        UserDetails userDetails = null;

        UserEntity userEntity = dao.findByName(username);

        if (userEntity == null)

            throw new UsernameNotFoundException("user not found");

        return assembler.buildUserFromUserEntity(userEntity);

    }

}

Here is the controller of the page I am trying to access:

public class HowDoesItWorkController implements Controller {

protected final Log logger = LogFactory.getLog(getClass());

@PreAuthorize("hasRole('ROLE_User')")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    logger.info("returning contact view");
    return new ModelAndView("/explain");
}

}

I have been following this tutorial:
http://giannisapi.wordpress.com/2011/09/21/spring-3-spring-security-implementing-custom-userdetails-with-hibernate/

Which says to insert those roles:

insert into security_role(name) values ("ROLE_admin");

insert into security_role(name) values ("ROLE_User");

I thought that the name of the role inserted in the DB should be the same as the one configured in the config file, so I changed the one in the xml file to fit the one in the DB but it doesn’t change anything.

All other data in the DB seems to be good.

I also verified that the page I am trying to access is in /user and it is the case.

  • 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-07T20:36:07+00:00Added an answer on June 7, 2026 at 8:36 pm

    I have been able to solve the problem. It was my mistake, I was not using the good database under the hibernate.cfg.xml file.

    The database that was configured here was referencing another database in which I also had the 3 tables, but because they were empty, no role were selected by the hibernate request.

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

Sidebar

Related Questions

I haven't been able to solve this prolog exercise. I was hoping someone here
Haven't been able to find this one out. How are Bitmaps stored in memory
I haven't been able to get this working and all of the sample code
I haven't worked with SQL Reporting much, however I have been trying to get
I haven't found a way to detect if user is currently viewing a page,
I haven't been able to find what these Xcode icons mean. Some you can
I haven't used the implements keyword before, and I've been trying to use it
I haven't been able to find authorative explanations, microformats or guidelines for the following,
I haven't really been able to find any good simple tutorials an animating a
Haven't been able to find a definite answer. I know that one button can

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.