1 回答
TA贡献1834条经验 获得超8个赞
对于遇到此问题的任何绝望的灵魂,这里是解决方案:
@RestController
public class MainLoginController {
@RequestMapping("/manuallogin")
ResponseEntity<Object> interceptLoginRequest ( ){
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
DefaultSavedRequest springSecuritySavedRequest = (DefaultSavedRequest) requestAttributes.getRequest()
.getSession()
.getAttribute( "SPRING_SECURITY_SAVED_REQUEST" );
queryString = springSecuritySavedRequest.getQueryString();
request.getSession().setAttribute( "queryString", queryString );
return ResponseEntity.status( HttpStatus.FOUND )
.location( URI.create( dinosaurServer.getLoginUrl() ) )
.build();
}
@RequestMapping("/handshakeWithDinosaur")
public ResponseEntity<Object> handshakeWithDinosaur ( String dinosaursToken ) {
Authentication authentication = this.authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
dino.getUser(), dino.getPass()
)
);
SecurityContext sc = SecurityContextHolder.getContext();
sc.setAuthentication( authentication );
request.getSession().setAttribute( SPRING_SECURITY_CONTEXT_KEY, sc );
String queryString = String.valueOf( request.getSession().getAttribute( "queryString" ) );
return ResponseEntity.status( HttpStatus.FOUND )
.location( URI.create( String.format( "%s?%s",SPRING_AUTH_ENDPOINT, queryString ) ) )
.build();
}
@Component
public class AuthProviderForDinosaur implements AuthenticationProvider {
@Override
public Authentication authenticate ( Authentication authentication ) throws AuthenticationException {
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add( new SimpleGrantedAuthority( "ROLE_USER" ) );
return new UsernamePasswordAuthenticationToken( authentication.getName(), authentication.getCredentials(), grantedAuths );
}
@Override
public boolean supports ( Class<? extends Object> authentication ) {
return ( UsernamePasswordAuthenticationToken.class.isAssignableFrom( authentication ) );
}
}
基本上,我启用了会话并让 Spring 在会话中为我保存请求,同时服务器与恐龙服务器对话并完成握手。完成后,向 Spring 询问先前请求的参数以通过 Spring Security 继续授权。
添加回答
举报