Получите фактические данные пользователя с помощью весенней загрузки - PullRequest
0 голосов
/ 05 апреля 2020

На самом деле я работаю над проектом форума, созданным с использованием Spring boot, Mongodb и Vue. js.

Когда я пытаюсь опубликовать новый комментарий и получить пользовательские данные с помощью SecurityContextHolder и приведение его к моему UsersDetailImpl, который реализует из класса UserDetails, предоставляемого загрузкой Spring, он выдает следующую ошибку: org.springframework.security.web.authentication.webauthenticationdetails не может быть приведен к .. .. UserDetailsImpl

Я действительно не знаю причину этой ошибки, потому что, если я тестирую ее из Postman, не сообщает об ошибке.

UserDetailsImpl. java

public class UserDetailsImpl implements UserDetails {
private static final long serialVersionUID = 1L;

private String id;

private String username;

private String email;

@JsonIgnore
private String password;

private Collection<? extends GrantedAuthority> authorities;

public UserDetailsImpl(String id, String username, String email, String password,
                       Collection<? extends GrantedAuthority> authorities) {
    this.id = id;
    this.username = username;
    this.email = email;
    this.password = password;
    this.authorities = authorities;
}

public static UserDetailsImpl build(User user) {
    List<GrantedAuthority> authorities = user.getRoles().stream()
            .map(role -> new SimpleGrantedAuthority(role.getName().name()))
            .collect(Collectors.toList());

    return new UserDetailsImpl(
            user.getId(),
            user.getUsername(),
            user.getEmail(),
            user.getPassword(),
            authorities);
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    return authorities;
}

public String getId() {
    return id;
}

public String getEmail() {
    return email;
}

@Override
public String getPassword() {
    return password;
}

@Override
public String getUsername() {
    return username;
}

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

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

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

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

@Override
public boolean equals(Object o) {
    if (this == o)
        return true;
    if (o == null || getClass() != o.getClass())
        return false;
    UserDetailsImpl user = (UserDetailsImpl) o;
    return Objects.equals(id, user.id);
}
}

CommentController. java

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/comments")
public class CommentController {
@Autowired
CommentRepository commentRepository;

@Autowired
RoleRepository roleRepository;

@PostMapping("/ask")
public ResponseEntity<?> ask (@Valid @RequestBody AskRequest askRequest) {

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();

    HashSet<String> strRoles = userDetails.getAuthorities().stream()
            .map(GrantedAuthority::getAuthority)
            .collect(Collectors.toCollection(HashSet::new));

    Set<Role> roles = new HashSet<>();
    strRoles.forEach(role -> {
        int cutPoint = role.indexOf("_");
        role = role.substring(cutPoint + 1).toLowerCase();

        findRole(roles, role, roleRepository);
    });

    User user = new User(userDetails.getUsername(),  userDetails.getEmail(), roles);
    ObjectId discussion_id = ObjectId.get();
    String slug =  new Slugify().slugify(askRequest.getTitle());

    Comment comment = new Comment(discussion_id, askRequest.getTitle(),
            askRequest.getText(),slug, "full_slug_test", Instant.now(),user);

    String info = comment.getDiscussion_id().toString() +  comment.getPosted() + comment.getTitle()
            + comment.getText() + comment.getAuthor().getUsername() + comment.getAuthor().getEmail()
            + comment.getAuthor().getId() + comment.getAuthor().getRoles();

    commentRepository.save(comment);

    return ResponseEntity.ok(new MessageResponse(info));
}
}

Я новичок во всех этих технологиях, могут быть серьезные ошибки. Все советы будут мне очень полезны, потому что проект академический c.

Если кому-то нужна дополнительная информация, просто спросите ее.

Спасибо всем:)

Ответы [ 2 ]

1 голос
/ 05 апреля 2020

Изменить authentication.getDetails() на getAuthentication().getPrincipal()

У вас будет: UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();

0 голосов
/ 06 апреля 2020

Наконец-то я обнаружил ошибку, и она была на внешней стороне. Я отправлял заголовки de с помощью JWT таким образом.

import axios from 'axios';
import authHeader from './auth-header';

const API_URL = 'http://localhost:8080/comments/';

class CommentsService {
ask(post){
    return axios.post(API_URL + 'ask', {
        title: post.title,
        text: post.text,
        headers: authHeader()
    });
  }
}
export default new CommentsService();

, и это совершенно неправильно, поэтому я нашел способ сделать это.

import axios from 'axios';
import authHeader from './auth-header';

const API_URL = 'http://localhost:8080/comments/';

class CommentsService {
ask(post){
    return axios.post(API_URL + 'ask', {
        title: post.title,
        text: post.text
    },{headers: authHeader()});
  }
}
export default new CommentsService();

Я также добавляю код в смонтировать заголовки.

export default function authHeader() {
let user = JSON.parse(localStorage.getItem('user'));

if (user && user.accessToken) {
  return { Authorization: 'Bearer ' + user.accessToken };
} else {
  return {};
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...