Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Securing AngularJS SPA with Spring Security 3.2

Any help, advice and experience is welcome.

Im currently having a separate AngularJS SPA on a Apache HTTP Server and a Spring Backend on a Tomcat 7 Servlet. The backend serves as a Rest API for the SPA. Some rest resources will require a user to have a certain role.

I've been searching the internet for days on what and how to implement the best security strategy:

  • Basic Auth
  • Digest
  • oAuth
  • Stateless, Cookies? Sessions? Tokens? CSRF?

How would you go about communicating Spring Security in Json or XML to your SPA to show the user an authentication page or an "your successfully authenticated page"?

Any help is appreciated.

like image 437
Vinchenzo Avatar asked Jan 25 '26 18:01

Vinchenzo


1 Answers

I finally figured out how to make the SPA authenticate with my Rest Backend.

In spring security I created a

  • Custom SimpleUrlAuthenticationFailureHandler which returns a HTTP-Unauthorizated if a login attempt fails.
  • Custom SavedrequestAwareAuthenticationSuccessHandler which returns Http-Oke if a login attempt is successful.
  • Custom AuthenticationEntryPoint which returns Http-Unauthorizated instead of a redirect.
  • Custom LogoutSuccessHandler which returns Http-OK.
  • I disabled CSRF.

If anyone needs more help feel free to let me know or message me.

like image 102
Vinchenzo Avatar answered Jan 28 '26 09:01

Vinchenzo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!