我有一個(gè)在查詢字符串中有一個(gè)令牌的 Web 方法,每個(gè)方法都使用這個(gè)令牌來識(shí)別請求的用戶。為此,我創(chuàng)建了一個(gè)名稱為 AuthenticationFilter 的 ResourceFilter,它從請求中獲取令牌并從數(shù)據(jù)庫中檢索用戶信息,如果用戶有效,則允許執(zhí)行該方法,否則會(huì)引發(fā)未授權(quán)異常。我的問題是:如何訪問在 AuthenticationFilter 類中檢索到的用戶信息我的網(wǎng)絡(luò)API:@GET@Consumes(MediaType.TEXT_PLAIN)@Produces({MediaType.APPLICATION_JSON})@Path("/getUserOrders")@ResourceFilters({AuthenticationFilter.class, AllowOroginFilter.class})public String getUserOrders(@Context UriInfo uriInfo) { //I need to access Usr object that retrived in AuthenticationFilter /*User Usr = AuthenticationFilter.Usr;*/ String Result = getUserOrders(Usr); return Result;}資源過濾器代碼:public class AuthenticationFilter implements ResourceFilter, ContainerRequestFilter, ContainerResponseFilter { public User usr = null; @Override public ContainerRequest filter(ContainerRequest containerRequest) { usr = AuthenticationUtiity.AuthenticateRequest(containerRequest); if(usr == null){ Response.ResponseBuilder builder = null; String response = "{\"Success\":false, \"Message\":\"Invalid username or password\"}"; builder = Response.status(Response.Status.UNAUTHORIZED).entity(response); throw new WebApplicationException(builder.build()); } return containerRequest; } }我在谷歌上搜索并找到了一種方法可以使用 ThreadLocal 并將用戶保存到 ThreadLocal 并在該線程中的任何位置獲取它。https://veerasundar.com/blog/2010/11/java-thread-local-how-to-use-and-code-sample/有沒有更簡單的方法來做到這一點(diǎn)?謝謝
添加回答
舉報(bào)
0/150
提交
取消