Spring Security - Form Login
Configure Spring Security using HttpSecurity
In Spring Security’s WebSecurityConfigurerAdapter class, you can customize form login in .config(HttpSecurity) method
Form Login Configuration Example
Here is an example configuration for Form Login.
SecurityConfiguration.java
1 | import org.springframework.context.annotation.Bean; |
LoginController.java
1 |
|
This controller simply returns login page.
Note that you need to add Thymeleaf Dependency so that you can use thymeleaf template. For Gradle, the dependency will be
1 | implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' |
classpath:templates/login.html
1 |
|
This is a very simple Login page with CSRF support. This page is using Thymeleaf template engine so taht csrf token can be sent to client.
About HttpSecurity Object
- Use java doc to get more details on HttpSecurity usage.
- HttpSecurity can configure authorization using antMatchers and set the login method(form, basic, openID, oauth2, saml) base on the request
- you can also config login process, logout process, saml2 login, oauth2 login, session management, csrf, configure filter etc
You can tell Spring Security to use form login authentication by calling HttpSecurity.formLogin()
method. HttpSecurity.formLogin() method returns FormLoginConfigurer.
FormLoginConfigurer
FormLoginConfigurer common methods
- loginPage(String)
- loginProcessingUrl(String)
- defaultSuccessUrl(String, boolean)
- defaultForwardUrl(String)
- successHandler(AuthenticationSuccessHandler)
- failureUrl(String)
- failureForwardUrl(String)
- failureHandler(AuthenticationFailureHandler)
- usernameParameter(String)
- passwordParameter(String)
- permitAll()
loginPage and loginProcessingUrl
By default login page and login processing url are both /login. We can customize the login page and login processing url.
Here we use /login.html as login page and use the default login processing url.
1 | .loginPage("/auth/login") |
defaultSuccessUrl and successForwardUrl
defaultSuccessUrl method sets the default url to be redirect to. defaultSuccessUrl method accespts second parameter alwaysUse. if alwaysUse is set to true, then logged in user will always be redirected to successUrl. This has the same effect as successForwardUrl() method.
1 | // just set a defaultSuccessUrl |
failureUrl() and failureForwardUrl() method are similar. They are used to redirect page when login fails.
usernameParameter and passwordParameter
The default username parameter is “username”. The default password parameter is “password”. You can customize both
1 | .usernameParameter("username") // default is username |
permitAll
Ensures the urls for failureUrl(String) as well as for the HttpSecurityBuilder, the getLoginPage() and getLoginProcessingUrl() are granted access to any user.
1 | permitAll() |
Redirect to Different page after Successful login
In some cases, you want to redirect to different page base on the user login. If the user has role ‘ROLE_ADMIN’, redirect to ‘/admin’ page, else redirect to ‘/‘.
You can’t do this with defaultSuccessUrl
method above, you need to create your own AuthenticationSuccessHandler. AuthenticationSuccessHandler is used after user successfully login.
You can follow the example of SimpleUrlAuthenticationSuccessHandler and create your own AuthenticationSuccessHandler.
CustomAuthenticationSuccessHandler.java
1 | package com.example; |
To use the CustomAuthenticationSuccessHandler, create a bean and config using .successHandler(customAuthenticationSuccessHandler())
method.
1 |
|
If front-end and back-end are separated, you want to return JSON instead of redirect when the user logs in. In this case, you can also use AuthenticationSuccessHandler to send JSON response. Do not use defaultSuccessUrl() and successForwardUrl() method because they cause redirect.
CSRF
You can choose to enable or disable CSRF toekn. The default is to enable CSRF so that attacker cannot use csrf attack. We can call HTTPSecurity.csrf().disable()
to disable it.
After you enable CSRF, you need to add a hidden field in the login form that contains csrf token value.
1 | <input type="hidden" |
We will discuss more on CSRF in the future post.
Logout
You can also use HttpSecurity to customize logout page and logout process. HttpSecurity’s logout()
method returns a LogoutConfigurer that you can use to customize logout.
LogoutConfigurer important methods
- logoutUrl(String)
- logoutSuccessUrl(String)
- addLogoutHandler(LogoutHandler)
- deleteCookies(String…)
- permitAll()
Here is an example configuration for Logouts
1 | http.logout() |
logoutUrl method sets the URL that triggers logout to occur. The default is a POST “/logout” request. If you want to use GET, use .logoutRequestMatcher(new AntPathRequestMatcher("/custom-logout", "GET"))
logoutSuccessUrl method sets the Url to redirect to after logout has occurred. The default is “/login?logout”
Source Code - https://github.com/xinghua24/SpringBootExamples/tree/master/FormLogin
Reference