Securing your REST API with basic authentication
To be able to authenticate and authorize users, we will add basic authentication implementation. By default, Spring will handle /login
as a web page and that is something we don't want for our REST API. Create a new class inside the security
package and call it WebSecurityEntryPoint
.
This class must implement the AuthenticationEntryPoint
interface whose purpose is to commence an authentication scheme. The implementation will look like this:
package com.journaler.api.security import org.springframework.security.core.AuthenticationException import org.springframework.security.web.AuthenticationEntryPoint import org.springframework.stereotype.Component import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse @Component class WebSecurityEntryPoint : AuthenticationEntryPoint { override fun commence( request: HttpServletRequest?, response: HttpServletResponse?, ...