in Education by
So I am trying to creating simple Login Page with help of the Spring Boot and Security. So what I currently have is custom login page and in memory authentication with one user and role. The problem is that when I am entering proper user/password spirng is not authenticating it as valid data, and redirecting me again to login page, but this time with: /login#error On client side I am using thymeleaf. form snippet:

Configuration class: @Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication()// .withUser("root").password("root").roles("USER"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() // .antMatchers("/").hasRole("USER") // .antMatchers("/login*").permitAll().and() // .formLogin() // .loginPage("/login")// .loginProcessingUrl("/perform_login")// .and()// .logout()// .permitAll(); } } And controller is simple @Controller @RequestMapping("/login") public class LoginController { @GetMapping public String getLoginPage() { return "login"; } } Any idea what I am doing wrong? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The easiest way (for me) is to make a @Bean and use WebSecurityConfigurerAdapter and implement WebMvcConfigurer to configure everything with few steps. This is the smallest example ever made: @Configuration @SpringBootApplication public class DemoApplication implements WebMvcConfigurer { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Controller public class HomeController { @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Model model, Authentication authentication, Principal principal) { if (authentication == null) { return "forward:/login"; } else { model.addAttribute("user", principal.getName()); return "home"; } } } @Bean public WebSecurityConfigurerAdapter webSecurityConfig() { return new WebSecurityConfigurerAdapter() { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable().authorizeRequests() .anyRequest().authenticated() .and().formLogin().loginPage("/login") .defaultSuccessUrl("/") .permitAll() .and().logout().permitAll(); http.headers().frameOptions().disable(); } @Override protected void configure(AuthenticationManagerBuilder builder) throws Exception { builder.authenticationProvider(new AuthenticationProvider() { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getName(); String password = authentication.getCredentials().toString(); if (username.equals("username") && password.equals("password")) { List grantedAuthorities = new ArrayList<>(); grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER")); return new UsernamePasswordAuthenticationToken(username, password, grantedAuthorities); } throw new AuthenticationServiceException("Invalid credentials."); } @Override public boolean supports(Class<?> authentication) { return authentication.equals(UsernamePasswordAuthenticationToken.class); } }); } }; } public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/home").setViewName("home"); registry.addViewController("/login").setViewName("login"); } } You said that you are using thymeleaf so lets say your login.html form will be something like this: <!doctype html> login example

And the home.html page when you are logged in: <!doctype html> home page Hello,
sign out

Related questions

0 votes
    I'm new to flutter and can't resolve the issue. Your application could not be compiled, because its ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I'm new to flutter and can't resolve the issue. Your application could not be compiled, because its ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 15, 2022 in Education by JackTerrance
0 votes
    I have some wizard-like pages in my application, its keeping state while navigating between small steps, the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have minified all my js files using require, but require created a minified js file( main.min.js ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I have minified all my js files using require, but require created a minified js file( main.min.js ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    When I try to open the Plesk Login Page via :8443 I get the Error: ERROR: Uncaught Error: Class ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I am developing a .NET core application with Identity login which provides a Bootstrap form In the /Identity ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I am following up with the Wirecloud User Guide available here https://wirecloud.readthedocs.io/en/latest/ ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Thanks for helping. I am trying to develop a static carousel (it won't move, but it acts like one) that stretches ... screen (but hidden, so no scrollbar). Here is the markup:...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Which one of the following principles of cyber security refers that the security mechanism must be as small and ... Economy of the Mechanism Least privilege Fail-safe Defaults...
asked Mar 6, 2021 in Technology by JackTerrance
0 votes
    I have a GWT application that I am trying to login using spring security. Currently, after a successful ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have implemented Spring Security in my application. I have used default implementation, i.e., I have ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    How Is Spring Security Implemented In a Spring Boot Application?...
asked Nov 15, 2020 in Technology by Editorial Staff
...