What I am trying to achieve: User can sign in with our project's sign in page (within project) without redirection to keycloak's default login page. I have configured spring security with Keycloak it is working fine But user logs in through keycloak's default login page
My questions: how can i achieve this feature where I will get token from Keycloak using REST API like
curl \
-d "client_id=id-client" \
-d "username=username" \
-d "password=psw" \
-d "grant_type=password" \
-d "client_secret=secret" \
"http://localhost:8080/auth/realms/myRealmName/protocol/openid-connect/token"
and give access to my Spring Project (Spring security whatever)
As far as i understand I can log in using jquery in my front end and obtain token, eventually pass to spring security or whatever
Any help would be appreciated
We have found good solution to this problem which I am going to explain step by step: First of all, if you want to use custom login page you have two options: 1. Modifying the existing keycloak themese like login/registration/passwordupdate which can be found via directory of /keycloak/themes/* 2. This can be a bit tricky - which can be achieved by modifying AuthenticationProvider of Spring Security in your project.
override fun configure(http: HttpSecurity?) {
http
?.authorizeRequests()
?.antMatchers("/**")?.authenticated()
?.and()
?.authenticationProvider(myAuthenticationProvider)
?.formLogin()
?.loginPage("/login")
?.successHandler { request, response, authentication -> redirectStrategy.sendRedirect(request, response, "/main")}
?.permitAll()
?.usernameParameter("username") //the username parameter in the queryString, default is 'username'
?.passwordParameter("password") //the password parameter in the queryString, default is 'password'
?.and()
?.logout()
?.logoutUrl("/logout") //the URL on which the clients should post if they want to logout
?.invalidateHttpSession(true)
?.and()
?.exceptionHandling()
}
MyAuthenticationProvider you should override this spring security class
One more thing I have asked in above question, what if i use rest api to access to spring project, in this case you should implement KeycloakWebSecurityConfigurerAdapter instead of WebSecurityConfigurerAdapter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With