Tôi có đoạn code sau (cố gắng đăng nhập người dùng trong programatically):mùa xuân Authentication.setAuthenticated (boolean) java.lang.IllegalArgumentException: Không thể thiết lập thẻ này để tin cậy
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
...
User tempUser = new User(correctUsername,
correctPassword,
true, true, true, true, // logging them in...
authorities // type is List<GrantedAuthority>
);
...
Authentication authentication
= new UsernamePasswordAuthenticationToken(tempUser, authorities);
// I'm using authorities again (List<GrantedAuthority>)
// is this the right spot for it?
...
// this is the line causing the error
authentication.setAuthenticated(true);
Khi tôi cố gắng chạy mà tôi nhận được như sau:
java.lang.IllegalArgumentException: Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead
Lưu ý rằng tôi đang sử dụng danh sách authorities
của GrantedAuthority
s cả trong User
và Authentication
đối tượng. Tôi không chắc mình nên sử dụng chúng ở đâu. Tôi đang cố gắng để nhân rộng câu trả lời cho another SO question nhưng đang chạy vào ngoại lệ được đăng ở trên. câu hỏi tương tự khác mà không khá trả lời câu hỏi của tôi:
- How to programmatically log user in with Spring Security 3.1
- Programmatically login in a user using spring security
Sau khi một số tìm kiếm gần nhất tôi đã tìm thấy một câu trả lời là tại the forum at springsource.org, và người đó bằng cách sử dụng một deprecated method, nhưng đó là một cách tiếp cận tương tự. Làm thế nào tôi có thể đăng nhập một người dùng theo cách lập trình?
Dòng nào gây ra sự cố? –
người cuối cùng; với bình luận ở trên nó nói rằng 'đây là dòng gây ra lỗi ('authentication.setAuthenticated (true);') – Josh