filmov
tv
Spring Boot Security Tutorial | REST & Web

Показать описание
In this Spring Boot Security tutorial I show you how to secure your RESTful endpoints & controllers with spring security.
I first start off my creating a standard spring boot project and add a couple of RESTful endpoints via spring’s @RestController and @RequestMapping annotations. At this stage spring security is not on the classpath so the URLs are all open to HTTP requests.
I then add the spring-boot-starter-security dependency to the classpath via a simple maven dependency and perform a project update. If spring security is on the classpath then the web application will automatically be secured with basic authentication on all HTTP endpoints when there is no bean annotated with @EnableWebSecurity. The default username and password is ‘user’ and a random UUID password is generated each time the application starts, you can see this in the start-up logs. Do not use this in production.
After showing the automatic spring security basic authentication on all endpoints, I then create a java class annotated with @EnableWebSecurity. The class SecurityConfig extends WebSecurityConfigurerAdapter and overrides the configure(HttpSecurity http) method. This method disables the default HTTP Spring Security and allows us to define our own fine grained http security on our RESTful endpoints. I specified that the ant path expressions endpoints must have the defined roles associated with the login credentials.
Within the bean annotated with EnableWebSecurity, I added and autowired an configureGlobal(AuthenticationManagerBuilder auth) method. This allows you to specify the type of authentication manager (LDAP, In-memory, Token based etc). I added two users with different roles and demo the application returning an http 403 unauthorized when an authenticated but unauthorized user attempted to access a specific endpoint.
I hope you enjoyed this introductory spring security with spring boot example! Give it a go!
Don’t forget to subscribe for more great tech videos :)
Till next time,
Phil
Filmed at Newcastle County Down, Slieve Donard. Mourne Mountains.
Links:
I first start off my creating a standard spring boot project and add a couple of RESTful endpoints via spring’s @RestController and @RequestMapping annotations. At this stage spring security is not on the classpath so the URLs are all open to HTTP requests.
I then add the spring-boot-starter-security dependency to the classpath via a simple maven dependency and perform a project update. If spring security is on the classpath then the web application will automatically be secured with basic authentication on all HTTP endpoints when there is no bean annotated with @EnableWebSecurity. The default username and password is ‘user’ and a random UUID password is generated each time the application starts, you can see this in the start-up logs. Do not use this in production.
After showing the automatic spring security basic authentication on all endpoints, I then create a java class annotated with @EnableWebSecurity. The class SecurityConfig extends WebSecurityConfigurerAdapter and overrides the configure(HttpSecurity http) method. This method disables the default HTTP Spring Security and allows us to define our own fine grained http security on our RESTful endpoints. I specified that the ant path expressions endpoints must have the defined roles associated with the login credentials.
Within the bean annotated with EnableWebSecurity, I added and autowired an configureGlobal(AuthenticationManagerBuilder auth) method. This allows you to specify the type of authentication manager (LDAP, In-memory, Token based etc). I added two users with different roles and demo the application returning an http 403 unauthorized when an authenticated but unauthorized user attempted to access a specific endpoint.
I hope you enjoyed this introductory spring security with spring boot example! Give it a go!
Don’t forget to subscribe for more great tech videos :)
Till next time,
Phil
Filmed at Newcastle County Down, Slieve Donard. Mourne Mountains.
Links:
Комментарии