Spring Security page Jump failure solution

Time:2020-12-11

This article mainly introduces how to solve the problem of spring security jump page failure. In this paper, the example code is introduced in detail, which has certain reference learning value for everyone’s study or work, and friends in need can refer to it

Today, I started the new springboot project and added spring security for the first time. After successfully starting the project, I found that it was a little different from the previous project. No matter how I set the system home page, the browser opened the login interface, as shown in the figure below:

No matter how I set the jump path of the controller, it doesn’t work. I’m so angry that I scratch my head!!!

After consulting various materials, it is found that it may be the reason for spring security permission control, so we set about configuring the control permission.

Create a new securityconfig class to configure permissions. The code is as follows:

import org.springframework.context.annotation.Bean;
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.crypto.password.NoOpPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    //Configure the resource file, where / CSS / * *, index can be accessed at will
    http
        .authorizeRequests()
        .antMatchers("/css/**", "/index").permitAll();
  }

}

Some explanations:

  • Authorization requests: configure the authorization authentication of some resources or links
  • Ant matchers: configure which resources or links need to be authenticated
  • Permitall: set the fully accessible resources or links

After adding the above permission settings, the index page can be accessed normally

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.

Recommended Today

Java Engineer Interview Questions

The content covers: Java, mybatis, zookeeper, Dubbo, elasticsearch, memcached, redis, mysql, spring, spring boot, springcloud, rabbitmq, Kafka, Linux, etcMybatis interview questions1. What is mybatis?1. Mybatis is a semi ORM (object relational mapping) framework. It encapsulates JDBC internally. During development, you only need to pay attention to the SQL statement itself, and you don’t need to […]