Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. $.ajax({
  2. url: "/user",
  3. type: "GET",
  4. contentType: "application/json; charset=utf-8",
  5. dataType: "json",
  6. headers: "Authorization": token,
  7. success: function (data, textStatus, jqXHR) {
  8. var $userInfoBody = $userInfo.find("#userInfoBody");
  9.  
  10. $userInfoBody.append($("<div>").text("Username: " + data.username));
  11. $userInfoBody.append($("<div>").text("Email: " + data.email));
  12.  
  13. var $authorityList = $("<ul>");
  14. data.authorities.forEach(function (authorityItem) {
  15. $authorityList.append($("<li>").text(authorityItem.authority));
  16. });
  17. var $authorities = $("<div>").text("Authorities:");
  18. $authorities.append($authorityList);
  19. $userInfoBody.append($authorities);
  20. $userInfo.show();
  21. }
  22. });
  23.  
  24. httpSecurity
  25. // we don't need CSRF because our token is invulnerable
  26. .csrf()
  27. .disable()
  28. .exceptionHandling()
  29. .authenticationEntryPoint(unauthorizedHandler)
  30. .and()
  31. // don't create session
  32. .sessionManagement()
  33. .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  34. .and()
  35. .authorizeRequests()
  36. // .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
  37. // allow anonymous resource requests
  38. .antMatchers(HttpMethod.GET, "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js")
  39. .permitAll().antMatchers("/auth/**").permitAll().antMatchers("/vendors/**").permitAll()
  40. .antMatchers("/production/images/**").permitAll().anyRequest().authenticated().and().formLogin()
  41. .loginPage("/login").loginProcessingUrl("/loginprocess").failureUrl("/?loginFailure=true").permitAll();
  42.  
  43. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
  44. ServletException {
  45. HttpServletRequest httpRequest = (HttpServletRequest) request;
  46. String authToken = httpRequest.getHeader(this.tokenHeader);
  47. // authToken.startsWith("Bearer ")
  48. // String authToken = header.substring(7);
  49. String username = jwtTokenUtil.getUsernameFromToken(authToken);
  50. System.out.println("Token is " + authToken);
  51. System.out.println("Username is " + username);
  52. System.out.println("Audience is from " + jwtTokenUtil.getAudienceFromToken(authToken));
  53. if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
  54. UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
  55. System.out.println(userDetails.getAuthorities());
  56. if (jwtTokenUtil.validateToken(authToken, userDetails)) {
  57. UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
  58. userDetails, null, userDetails.getAuthorities());
  59. authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));
  60. SecurityContextHolder.getContext().setAuthentication(authentication);
  61. } else {
  62. System.out.println("Token is invalid ");
  63. }
  64. }
  65. chain.doFilter(request, response);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement