Заполнить коллекцию <GrantedAuthority>Полномочия от лица Роль - PullRequest
0 голосов
/ 19 апреля 2020

Я новичок ie весной. Я делаю авторизацию с классом MyUserPrincipal.

Кодовый класс MyUserPrincipal:

package com.greatproject.dishonline.service;

import applicationContext.SpringApplicationContext;
import com.greatproject.dishonline.entity.Role;

import com.greatproject.dishonline.entity.User;

import java.util.*;
import java.util.stream.Collectors;

import com.greatproject.dishonline.repository.RolesRepository;
import com.greatproject.dishonline.repository.UsersRepository;
import org.jvnet.hk2.annotations.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

public class MyUserPrincipal  implements UserDetails {

    @Autowired
  RolesRepository rolesRepository

    RegistrationService registrationService=(RegistrationService) SpringApplicationContext.getBean("RegistrationService");

    private User user;

   // private List<RoleAuth> roles;
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public MyUserPrincipal(User user) {
        this.user = user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
       Collection<GrantedAuthority> authorities = new ArrayList<>();

       for (Role role: rolesRepository.findRolesNames()
             ) {

            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getName());
            authorities.add(authority);

        }

        return authorities;

    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getLogin();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return user.isActive();
    }
    //...
}

Но @Awtowired не работает в классе MyUserPrincipal ( roleRepository.findRolesNames () возвращает Null), потому что он не аннотирует @controller или @service. Пожалуйста, помогите мне, как заполнить полномочия по сбору от роли лица без использования @ Awtowired.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...