SHARE
TWEET

Untitled

a guest Jan 26th, 2019 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Component
  2. public class CorsFilter extends OncePerRequestFilter {
  3.  
  4.     @Override
  5.     protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
  6.                                     final FilterChain filterChain) throws ServletException, IOException {
  7.         response.addHeader("Access-Control-Allow-Origin", "*");
  8.         response.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH, HEAD");
  9.         response.setHeader("Access-Control-Allow-Headers", "X-Requested-With, Authorization, Content-Type");
  10.         response.addIntHeader("Access-Control-Max-Age", 3600);
  11.  
  12.         if ("OPTIONS".equalsIgnoreCase(request.getMethod()))
  13.         {
  14.             response.setStatus(HttpServletResponse.SC_OK);
  15.         }
  16.         else
  17.         {
  18.             filterChain.doFilter(request, response);
  19.         }
  20.     }
  21. }
  22.    
  23. @Override
  24.     protected void configure(HttpSecurity http) throws Exception
  25.     {
  26.         http.addFilterBefore(corsFilter(), SessionManagementFilter.class);
  27.         http.csrf().disable().
  28.             authorizeRequests()
  29.                 .antMatchers("/api/auth/**","/api/MentorCalendar","/api/user/role/**").permitAll()
  30.                 .anyRequest().authenticated()
  31.                 .and()
  32.                 .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
  33.                 .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  34.  
  35.         http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
  36.     }
  37.    
  38. @Component
  39. public class CorsFilter extends OncePerRequestFilter {
  40.  
  41.     @Override
  42.     protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
  43.                                     final FilterChain filterChain) throws ServletException, IOException {
  44. //        response.addHeader("Access-Control-Allow-Origin", "*");
  45.         response.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH, HEAD");
  46.         response.setHeader("Access-Control-Allow-Headers", "X-Requested-With, Authorization, Content-Type");
  47.         response.addIntHeader("Access-Control-Max-Age", 3600);
  48.  
  49.         if ("OPTIONS".equalsIgnoreCase(request.getMethod()))
  50.         {
  51.             response.setStatus(HttpServletResponse.SC_OK);
  52.         }
  53.         else
  54.         {
  55.             filterChain.doFilter(request, response);
  56.         }
  57.     }
  58. }
  59.    
  60. export class LoginComponent {
  61.   constructor(private LoginService: LoginService, private router: Router) {}
  62.  
  63.   login(inputUsername, inputPassword){
  64.     this.LoginService.login({username: inputUsername, password: inputPassword})
  65.         .subscribe(data => {
  66.         localStorage.setItem("jwtToken", data.accessToken);
  67.    
  68. const httpOptions = {
  69.   headers: new HttpHeaders({
  70.     'Content-Type':  'application/json'
  71.   })
  72. };
  73.  
  74. @Injectable()
  75. export class LoginService {
  76.   loginUrl = "http://localhost:8924/api/auth/signin";
  77.  
  78.   constructor( private http: HttpClient ) {}
  79.  
  80.   login(loginCreds: any): Observable<any> {
  81.   return this.http.post<any>(this.loginUrl, loginCreds, httpOptions);
  82.   }
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top