Skip to main content

spring boot CORS filter along with spring security and OAuth2 (@EnableOAuth2Sso)

spring boot CORS filter along with spring security and OAuth2 (@EnableOAuth2Sso)

import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;



@Component
public class SimpleCrosFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Headers", "authorization, content-type, xsrf-token");
        response.addHeader("Access-Control-Expose-Headers", "xsrf-token");

                if ("OPTIONS".equals(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);

        } else {
                    chain.doFilter(req, res);
        }



    }

    @Override
    public void destroy() {

    }
}



spring security filter : here the key thing is you have to permitall preflight request by adding requestMatchers(CorsUtils::isPreFlightRequest).permitAll()


            http
                    .antMatcher("/**").authorizeRequests().requestMatchers(CorsUtils::isPreFlightRequest).permitAll().antMatchers("/","/user**", "/login**", "/webjars/**" )
                    .permitAll().anyRequest()
                    .authenticated().and().exceptionHandling()
                    .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout()
                    .logoutSuccessUrl("/").permitAll().and()
                    .csrf().disable()
                    .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. It is really a great and useful piece of info. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thank you for sharing.
    GRE Coaching in Chennai

    ReplyDelete
  3. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me.. iOS App Development Company in Chennai

    ReplyDelete
  4. Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.

    AWS Training in Chennai

    ReplyDelete
  5. thank you for your content very unique blog.one of the recommanded blog for learners and professionals.

    Java training in hyderabad

    ReplyDelete
  6. I am not sure the place you are getting your information, however good topic.I needs to spend some time studying more or understanding more.Thank you for wonderful information I was in search of this info for my mission.

    Manpower Consultancy in Bangalore
    HR Consultancy in Bangalore
    Recruitment Consultancy in Bangalore
    HR Franchise in Bangalore

    ReplyDelete

  7. Your blog is very useful for me, as your tutorial sessions are indeed of great benefit.java training in chennai | chennai's no.1 java training in chennai | best java institute in chennai

    ReplyDelete
  8. I think it's awesome someone is finally taking notice of our vet's and doing something to help them. I hope all goes well with these articles. More new information i will get after refer that post.
    Interior Decorators in Chennai
    Home Interior Designers in Chennai
    Home Interiors in Chennai

    ReplyDelete
  9. Really Good blog post about spring boot cors.provided a helpful information.I hope that you will post more updates like this.
    Digital marketing company in Chennai

    ReplyDelete
  10. I’ve been browsing on-line greater than three hours today, but I never discovered any attention-grabbing article like yours. It is beautiful worth sufficient for me. Personally, if all webmasters and bloggers made good content material as you did, the net will be a lot more helpful than ever before.
    Architectural Firms in Chennai
    Architects in Chennai

    ReplyDelete
  11. Free easy & simple way to learn programming online we provide niit projects, assignments, cycle tests and much more..
    visit===>> http://foundjava.blogspot.in/

    ReplyDelete
  12. It's easy to understand spring boot corps filter along with spring security and oauth2 and code to implement it. I could learn it very effectively.Thanks a lot for sharing.


    Android app development company in chennai

    ReplyDelete
  13. I appreciate your style of writing because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also informative. I hope to read more and more interesting articles from your blog. All the best.
    Pigmentation Cream

    ReplyDelete
  14. Great.Nice information.It is more useful and knowledgeable. Thanks for sharing keep going on..
    SEO company in India
    Digital Marketing Company in Chennai

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete

Post a Comment