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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:40:19+00:00 2026-06-17T22:40:19+00:00

these days I meet a problem, I can not figure it out,so please help

  • 0

these days I meet a problem, I can not figure it out,so please help me…

My entity: Utilisateur this is a french word means user

    @Entity
    @Inheritance(strategy = InheritanceType.JOINED)
    public class Utilisateur implements Serializable 
    {
         private static final long serialVersionUID = 1L;
         @Id
         @GeneratedValue(strategy = GenerationType.IDENTITY)
         protected int id;
         protected String login;
         protected String password;
         protected String nom;
         protected String prenom;
         protected String email;
         protected String username;}

    @OneToOne(mappedBy="user", cascade={CascadeType.ALL})
    private Role role;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return login;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }



    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }
}

and a Role entity.

In my web app, there is a controller to show for example the information about a student(Etudiant in french)

@EJB(mappedName = "Etudiant.EtudiantFacade")
    EtudiantFacade etudiantF;

    // Affiche le detail d'un Etudiant  (show the infomations of the student)
    @RequestMapping(value = "/Etudiant/{idEtudiant}/info")
    public ModelAndView detail(@PathVariable String idEtudiant, Model m) {
        m.addAttribute("etudiant",
                etudiantF.trouver(Integer.parseInt(idEtudiant)));
        return new ModelAndView("EtudiantInformation", "null", null);
    }

I implemented my own CustomUseDetailService using the entity Utilisateur directly.

 @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException, DataAccessException {
        // TODO Auto-generated method stub
        System.out.println("!!!!!!!!!!!!!!!!!");
        System.out.println(username);

        boolean enabled = true;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = true;

            Utilisateur etudiant = etudiantF.trouverParLogin(username);


            return new User(etudiant.getLogin(), etudiant.getPassword(), enabled,accountNonExpired,credentialsNonExpired,accountNonLocked,getAuthorities(etudiant.getRole().getRole()));

    }

and my security.xml is below:

<http auto-config="true" use-expressions="true">
        <intercept-url pattern="/app/Login" access="permitAll"/>
        <intercept-url pattern="/app/*" access="hasAnyRole('ROLE_ADMIN','ROLE_USER')"/>
        <form-login login-page="/app/Login" 
            authentication-success-handler-ref="authenticationSuccessHandler"/> 
        <logout logout-url="/app/Logout" logout-success-url="/"/>
    </http>

    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="customUserDetailsService"/>
    </authentication-manager>

Last my question is:
for a student, his id is 1, his username is stu1,to control this student with id 1 can only access his own page information /ProjetName/Student/{studentId}/Info
how do I write the code with @PreAuthorize, I have see the document in form spring, there is example like @PreAuthorize(#contract.name = principal.username), because there is a attribute username in principal, but here,what I need is Id, I use @RequestMapping(value = "/Etudiant/{idEtudiant}/info") to match the student not the username. So how can I solve it? Many thanks… I can not find the tutorial.

  • 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-17T22:40:20+00:00Added an answer on June 17, 2026 at 10:40 pm

    You can provide your own implementation for User class (just extend org.springframework.security.core.userdetails.User). Add an identifier field to it. Then set corresponding value in loadUserByUsername method:

    @Override
    public UserDetails loadUserByUsername(String username)
        ...
        return new CustomUser(etudiant.getId(), etudiant.getLogin(), etudiant.getPassword(), enabled,accountNonExpired,credentialsNonExpired,accountNonLocked,getAuthorities(etudiant.getRole().getRole()));
    
    }
    

    Then you will be able to use it:

    @PreAuthorize(#contract.name = principal.id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

These days, I've seen a URL pattern like this: http://stackoverflow.com/questions/How-to-do Not a usual and
These days, we encountered a strange problem, some of our solr apps on tomcat
These days web addresses can also include non-ASCII characters. So every modern browser and
These days I'm working on a VB.NET application which can be used to edit,
The general consensus these days seems to be that you do not store binary
These days I'm facing a problem with encoding string from an array of byte.
I installed integrity app these days. and found some issues. I follow this. $
There is a lot of buzz these days about not using locks and using
I got one problem these days. I'm using an image-cache library, it works well
I'm learning these days how to use ant to run automated test folowing this

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.