1

这些天我遇到一个问题,我无法弄清楚,所以请帮助我......

我的实体: Utilisateur 这是一个法语单词,意思是用户

    @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;
    }
}

和一个Role实体。

在我的网络应用程序中,有一个控制器可以显示例如有关学生的信息(Etudiant法语)

@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);
    }

CustomUseDetailService我直接使用实体实现了我自己的Utilisateur

 @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()));

    }

我的security.xml如下:

<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>

最后我的问题是:对于一个学生,他的id是1,他的用户名是stu1,要控制这个id为1的学生只能访问他自己的页面信息 /ProjetName/Student/{studentId}/Info 我该如何编写代码@PreAuthorize,我已经看到了spring表单中的文档,有这样的例子@PreAuthorize(#contract.name = principal.username),因为principal中有一个属性username,但是在这里,我需要的是Id,我@RequestMapping(value = "/Etudiant/{idEtudiant}/info")用来匹配学生而不是用户名。那么我该如何解决呢?非常感谢...我找不到教程。

4

1 回答 1

1

您可以为 User 类提供自己的实现(只需扩展org.springframework.security.core.userdetails.User)。向其添加标识符字段。然后在loadUserByUsername方法中设置相应的值:

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

}

然后你就可以使用它了:

@PreAuthorize(#contract.name = principal.id)
于 2013-01-28T10:35:59.297 回答