I want to see the login page first even any user want access any other page need to redirect to login page in spring security

Sushovan Mallick

Here is the sample controller. My problem is basically when i am entering the base url it is redirecting to inner page not in the log in page. What i want. What should i do to achieve this.

Here is the sample controller. My problem is basically when i am entering the base url it is redirecting to inner page not in the log in page. What i want. What should i do to achieve this.

package com.sushovan.security.controller;

import javax.validation.groups.ConvertGroup;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

    @RequestMapping("/")
    public String home() {
        return "home.jsp";
    }

    @RequestMapping("/login")
    public String loginPage() {
        return "login.jsp";
    }

    @RequestMapping("/logout-success")
    public String logoutPage() {
        return "logout.jsp";
    }
}

Here is the sample Security Configuration class.Mostly all configuration have been done here.

package com.sushovan.security.config;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
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.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.util.AntPathMatcher;

@Configuration
@EnableWebSecurity
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;
    /**This is for authentication from database**/
    @Bean
    public AuthenticationProvider authProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();

        provider.setUserDetailsService(userDetailsService);
        //provider.setPasswordEncoder(NoOpPasswordEncoder.getInstance());//This is for not use any encryption
        provider.setPasswordEncoder(new BCryptPasswordEncoder());//This is for BCryptPasswordEncoder
        return provider;
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();
        http.authorizeRequests().antMatchers("/login")
            .permitAll()
            .and()
            .formLogin()
            .loginPage("/login")
            .usernameParameter("userName").passwordParameter("password")
            .permitAll()
            .and()
            .logout().invalidateHttpSession(true)
            .clearAuthentication(true)
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/logout-success").permitAll();

    }
}
Ali D.A.

Spring security filters algorithm works like this ; is web resource protected ? is user authenticated ? is user authorized ?

So if its not authenticated it redirect request to login page, which is what you want.

So you should update your configure method

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();
        http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .usernameParameter("userName").passwordParameter("password")
            .permitAll()
            .and()
            .logout().invalidateHttpSession(true)
            .clearAuthentication(true)
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/logout-success").permitAll();

    }

can you please try this and let me know if it works ?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring Security need to redirect to Login Page on BadCredentialsException

Django-allauth Redirect any unauthenticated user to login page

Unable to redirect a user to a desired page after login with Spring Security

Spring Security: Redirect to Login Page in case of 401

Spring security and angular javascript redirect to login page

Laravel - Login on any page

Spring Security Login Page

How unauthorize access redirect user to login page

Website from login page i want to direct to the main page?

Symfony security redirect to login page

Redirect User back to Page on Login

Redirect to login page if user is not logged in

How do I redirect an anonymous user to custom page instead of the login page if they do not have access to a URL?

How to redirect to the page that I want after logged in and display username after successful login

Spring security login page - images

Login page is not permitted in Spring Security

Spring Security no controller for login page

If i lost the JSF Session scope i want to change to the login page

I want to navigate to Another page after I click Login

My login page will allow any credentials to login

I am creating a simple application in which on successful login I want to navigate from login page to start page

Spring Security simple login page cant login

Spring Security - Cannot login with custom login page

I want to render website through login page in shinyApp

I want to go to the default page ("/") after login (django view problem)

I want to visit homepage as a guest but facing login page continuously in Laravel

Spring security redirecting to a page after login using redirect: uri

Preventing spring-security to redirect invalid urls to login page

Spring Security OAuth2 with JWT redirect to login page