我試圖在Guice中實施自定義驗證過濾器。我收到令牌,從令牌獲取用戶名和領域,然後創建一個委託人。現在我被卡住了,我不知道如何設置校長。如果我可以像這樣設置request.setUserPrincipal(principal);
就好了,但顯然我不能。在Guice過濾器中創建委託人
我該怎麼做?
我doFilter方法是這樣的:
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
if (authorizationHeader != null && authorizationHeader.length() > 0) {
String token = authorizationHeader.substring("Bearer".length()).trim();
if (token.length() > 0) {
try {
Credentials credentials = securityService.getCredentials(token);
String username = credentials.getUsername();
String realm = credentials.getRealm();
Principal principal = new HttpPrincipal(username, realm);
// request.setUserPrincipal(principal);
LOGGER.info(credentials);
} catch (Exception e) {
LOGGER.error(e);
}
}
}
filterChain.doFilter(servletRequest, servletResponse);
}