Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取特定用户的所有权限_Java_Spring_Spring Security - Fatal编程技术网

Java 获取特定用户的所有权限

Java 获取特定用户的所有权限,java,spring,spring-security,Java,Spring,Spring Security,我有一个用户实体,存在的每个用户都应该有一个或多个特定角色/权限。例如adminuser等等。 因此,我创建了一个带有两个字段的authorities表(username和authority) 在我的应用程序上下文securtiy.xml中 <jdbc-user-service id="userService" data-source-ref="dataSource" users-by-username-query=" select nickname, passwo

我有一个
用户
实体,存在的每个用户都应该有一个或多个特定角色/权限。例如
admin
user
等等。 因此,我创建了一个带有两个字段的
authorities
表(
username
authority
) 在我的
应用程序上下文securtiy.xml

<jdbc-user-service id="userService" data-source-ref="dataSource"
    users-by-username-query="
        select nickname, password, true from users where nickname=?"
    authorities-by-username-query= "select username, authority AS authorities 
    from authorities where username=?" 
/>
如何将数据从控制器发送到视图:

@RequestMapping(value = "/manageUsers", method = RequestMethod.GET)
public String startManageUsers(ModelMap model, Principal principal) {
    List<User> list = userService.getUsers();
    model.addAttribute("users", list);    
    return "showUsers";
}
@RequestMapping(value=“/manageUsers”,method=RequestMethod.GET)
公共字符串StartManager用户(模型映射模型,主体){
List=userService.getUsers();
model.addAttribute(“用户”,列表);
返回“showUsers”;
}

我认为最好的方法是实现UserDetailsService和UserDetails

public class UserLoginBean implements UserDetails {

    private static final long serialVersionUID = 1L;

    private Long id;
    private String fullname;
    private String username;
    private String password;
    private boolean locked;
    private Set<GrantedAuthority> authorities = null;

    private UserEntity userEntity;

    public UserLoginBean(Long id, String username, String password, boolean locked ) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.locked = locked;
    }

    public boolean isAccountNonExpired() {
        return true;
    }

    public boolean isAccountNonLocked() {
        return locked;
    }

    public boolean isCredentialsNonExpired() {
        return true;
    }

    public boolean isEnabled() {
        return true;
    }

    public Set<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities( Set<GrantedAuthority> authorities ) {
        if ( this.authorities == null ) {
            this.authorities = authorities;
        }
    }

    // setters, getters
    }
UserDetailsService的示例实现

@Service
@Transactional
public class UserLoginService implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
        UserEntity userEntity = this.userService.getUserByUserId(userId);
        if (userEntity == null) {
            throw new UsernameNotFoundException("User not found");
        }

        UserLoginBean bean = new UserLoginBean(userEntity.getId(), userEntity.getUserId(), userEntity.getPassword(), userEntity.getEnabled());
        bean.setFullname(userEntity.getFullname());
        bean.setUserEntity(userEntity);

        Set<GrantedAuthority> roles = new HashSet<GrantedAuthority>();
        roles.add( new SimpleGrantedAuthority( userEntity.getRole() ) );
        bean.setAuthorities(roles);

        return bean;
    }

}
@服务
@交易的
公共类UserLoginService实现UserDetailsService{
@自动连线
私人用户服务;
@凌驾
public UserDetails loadUserByUsername(字符串userId)引发UsernameNotFoundException{
UserEntity UserEntity=this.userService.getUserByUserId(userId);
if(userEntity==null){
抛出新的UsernameNotFoundException(“未找到用户”);
}
UserLoginBean=newUserLoginBean(userEntity.getId(),userEntity.getUserId(),userEntity.getPassword(),userEntity.getEnabled());
setFullname(userEntity.getFullname());
setUserEntity(userEntity);
Set roles=new HashSet();
添加(新的SimpleGrantedAuthority(userEntity.getRole());
设置权限(角色);
返回豆;
}
}
UserDetails的示例实现

public class UserLoginBean implements UserDetails {

    private static final long serialVersionUID = 1L;

    private Long id;
    private String fullname;
    private String username;
    private String password;
    private boolean locked;
    private Set<GrantedAuthority> authorities = null;

    private UserEntity userEntity;

    public UserLoginBean(Long id, String username, String password, boolean locked ) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.locked = locked;
    }

    public boolean isAccountNonExpired() {
        return true;
    }

    public boolean isAccountNonLocked() {
        return locked;
    }

    public boolean isCredentialsNonExpired() {
        return true;
    }

    public boolean isEnabled() {
        return true;
    }

    public Set<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities( Set<GrantedAuthority> authorities ) {
        if ( this.authorities == null ) {
            this.authorities = authorities;
        }
    }

    // setters, getters
    }
public类UserLoginBean实现UserDetails{
私有静态最终长serialVersionUID=1L;
私人长id;
私有字符串全名;
私有字符串用户名;
私有字符串密码;
私有布尔锁;
私有集权限=null;
私有用户实体用户实体;
public UserLoginBean(长id、字符串用户名、字符串密码、布尔锁定){
this.id=id;
this.username=用户名;
this.password=密码;
this.locked=locked;
}
公共布尔值IsAccountNoExpired(){
返回true;
}
公共布尔值isAccountNonLocked(){
返回锁定;
}
公共布尔值isCredentialsNonExpired(){
返回true;
}
公共布尔值isEnabled(){
返回true;
}
公共设置权限(){
返回当局;
}
公共权限(集合权限){
if(this.authorities==null){
这个.权威=权威;
}
}
//二传手
}
最后一件事是将spring安全性配置为UserLoginService作为身份验证提供者

例如:

<security:authentication-manager alias="authenticationManager">
      <security:authentication-provider user-service-ref="userLoginService">
      <security:password-encoder ref="userPasswordEncoder"/>
      </security:authentication-provider>
    </security:authentication-manager>

    <bean

    <bean id="userLoginService" class="com.stackoverflow.UserLoginService" />


您可以显示您的JSP代码吗?
items=“${user.getAuthorities('}”
?最后删除不匹配的paren和。另外,您可能需要
items=“${user.authorities}”
是的,这只是一个输入错误……我不知道如何实现它通常会以这种方式工作的东西如何向我们展示你的
User
类。如果你想让这个代码段工作,它应该有一个方法getAuthories。@soupDriver所以问题是你没有
getAuthories()
de.htw_berlin.f4.ai.kbe.glaeskeLensing.editorials.domain.User中的方法是否正确实现了
或者什么?抱歉,这在过去太远了,我不太喜欢这个项目,所以我无法验证您的答案:/
<%@ taglib prefix="a" uri="http://www.springframework.org/security/tags"%>

<a:authentication property="principal" var="principal" />
<c:if test="${not empty principal && principal != 'anonymousUser'}">
        <c:set var="fullname"><c:out value="${principal.fullname}" /></c:set>
    </c:if>