[英]How to add jwt authendication header with all requests in spring security?
我按照這個鏈接設置了jwt身份驗證。 它工作正常。 所有請求都是通過附加如下所示的身份驗證標頭從java腳本生成的。
$.ajax({
url: "/user",
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: "Authorization": token,
success: function (data, textStatus, jqXHR) {
var $userInfoBody = $userInfo.find("#userInfoBody");
$userInfoBody.append($("<div>").text("Username: " + data.username));
$userInfoBody.append($("<div>").text("Email: " + data.email));
var $authorityList = $("<ul>");
data.authorities.forEach(function (authorityItem) {
$authorityList.append($("<li>").text(authorityItem.authority));
});
var $authorities = $("<div>").text("Authorities:");
$authorities.append($authorityList);
$userInfoBody.append($authorities);
$userInfo.show();
}
});
但我希望這不是通過javascript附加到所有后續請求。
這是我的安全配置
httpSecurity
// we don't need CSRF because our token is invulnerable
.csrf()
.disable()
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.and()
// don't create session
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
// .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
// allow anonymous resource requests
.antMatchers(HttpMethod.GET, "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js")
.permitAll().antMatchers("/auth/**").permitAll().antMatchers("/vendors/**").permitAll()
.antMatchers("/production/images/**").permitAll().anyRequest().authenticated().and().formLogin()
.loginPage("/login").loginProcessingUrl("/loginprocess").failureUrl("/?loginFailure=true").permitAll();
這是我的身份驗證過濾器
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String authToken = httpRequest.getHeader(this.tokenHeader);
// authToken.startsWith("Bearer ")
// String authToken = header.substring(7);
String username = jwtTokenUtil.getUsernameFromToken(authToken);
System.out.println("Token is " + authToken);
System.out.println("Username is " + username);
System.out.println("Audience is from " + jwtTokenUtil.getAudienceFromToken(authToken));
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
System.out.println(userDetails.getAuthorities());
if (jwtTokenUtil.validateToken(authToken, userDetails)) {
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
userDetails, null, userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));
SecurityContextHolder.getContext().setAuthentication(authentication);
} else {
System.out.println("Token is invalid ");
}
}
chain.doFilter(request, response);
}
如何在身份驗證后將身份驗證標頭附加到服務器的響應中。 這樣所有后續請求都將自動與該認證頭一起使用。
我認為的方法是否正確? 還是有其他最好的方法嗎?
前端負責為每個請求添加標頭,而不是后端。 如果您不想使用JS執行此操作,則可以使用安全的httponly cookie。 當您從后端設置cookie時,它將在每個后續請求中包含它到它發出的域。
但是,使用cookie存在一些安全問題。 本文很好地解釋了安全性考慮因素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.