<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Welcome to Akash Home</title> <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css" /> <script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script> <script type="text/javascript" src="/webjars/bootstrap/js/bootstrap.min.js"></script> </head> <body> <div class="container text-center"> <h1>Welcome to the portal</h1> <h3> <a href="/register">Register</a> </h3> <h3> <a href="show-menu-list-admin">Login as a admin</a><br> <a href="show-menu-list-customer">Login as a user</a><br> <!-- <a href="login">login</a> --> <a href="logout">logout</a> </h3> </div> </body> </html>
here I am creating separate links for login as an admin/user. how to add a feature of a single login page that redirects to the next page according to the credential entered for ex: if user1 is an admin if his credentials are entered he will be redirected to the admin page and vise-versa for a user login
here is my spring security config code :-
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean public UserDetailsService getUserDetailService() { return new UserDetailsServiceImpl(); } @Bean public BCryptPasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean public DaoAuthenticationProvider authenticationProvider() { DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); daoAuthenticationProvider.setUserDetailsService(this.getUserDetailService()); daoAuthenticationProvider.setPasswordEncoder(passwordEncoder()); return daoAuthenticationProvider; } // authentication - configure method @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(authenticationProvider()); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/show-menu-list-admin").hasRole("ADMIN") .antMatchers("/show-menu-list-customer").hasRole("USER").and().formLogin().and().csrf().disable(); } }
Anonymous Asked question May 14, 2021
Recent Comments