首页 > 解决方案 > 如何禁用 Spring Boot 安全性

问题描述

场景: 将父 POM 扩展到子 POM。于是spring-boot-starter-security继承了。

通常,如果触发 ,http://localhost:9000/控件将到达定义以下方法的 Controller 类:

@GetMapping("/")
public String main(Model model, @RequestHeader HttpHeaders headers) {
     //body
}

包含Parent POM(嵌入了spring security)后,当触发上述url时,会弹出用户名和密码窗口。

我们如何才能绕过这种安全性。

Web模块中包含这个类:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
        PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        auth.inMemoryAuthentication()
                .withUser("spring")
                .password(encoder.encode("secret"))
                .roles("USER")
                .and()
                .withUser("user1").password(encoder.encode("password")).roles("USER")
                .and()
                .withUser("admin1").password(encoder.encode("password")).roles("ADMIN");
    }

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/anonymous*", "/error*").anonymous()
                .antMatchers("/").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .successHandler(successHandler())
                .and()
                .logout().deleteCookies("JSESSIONID")
                .and()
                .rememberMe().key("uniqueAndSecret").tokenValiditySeconds(86400)
                .and()
                .sessionManagement()
                .sessionFixation().migrateSession()
                .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                .invalidSessionUrl("/invalidSession.html")
                .maximumSessions(2)
                .expiredUrl("/sessionExpired.html");
    }
    private AuthenticationSuccessHandler successHandler() {
        return new SimpleUrlAuthenticationSuccessHandler();
    }
    @Bean
    public HttpSessionEventPublisher httpSessionEventPublisher() {
        return new HttpSessionEventPublisher();
    }
}

但它没有成功。 编辑: 父 POM:

<dependencies>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>${com.oracle.jdbc.ojdbc7.version}</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>com.sun.facelets</groupId>
        <artifactId>jsf-facelets</artifactId>
        <version>1.1.15</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>${maven.jersey.version}</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${maven.jersey.version}</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>${maven.jersey.version}</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${maven.jersey.version}</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>${maven.jersey.version}</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-apache-client</artifactId>
        <version>${maven.jersey.version}</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>${maven.jersey.version}</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-spring</artifactId>
        <version>${maven.jersey.version}</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey.jersey-test-framework</groupId>
        <artifactId>jersey-test-framework-inmemory</artifactId>
        <version>${maven.jersey.version}</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-grizzly</artifactId>
        <version>${maven.jersey.version}</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey.jersey-test-framework</groupId>
        <artifactId>jersey-test-framework-grizzly</artifactId>
        <version>${maven.jersey.version}</version>
        <scope>test</scope>
        <exclusions>
          <exclusion>
            <artifactId>glassfish-embedded-all</artifactId>
            <groupId>org.glassfish.embedded</groupId>
          </exclusion>
          <exclusion>
            <artifactId>servlet-api</artifactId>
            <groupId>javax.servlet</groupId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey.jersey-test-framework</groupId>
        <artifactId>jersey-test-framework-grizzly2</artifactId>
        <version>${maven.jersey.version}</version>
      </dependency>
      <dependency>
        <groupId>com.sun.jersey.jersey-test-framework</groupId>
        <artifactId>jersey-test-framework-external</artifactId>
        <version>${maven.jersey.version}</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-xjc</artifactId>
        <version>2.3.0</version>
      </dependency>
      <dependency>
        <groupId>com.sun.xml.stream.buffer</groupId>
        <artifactId>streambuffer</artifactId>
        <version>1.4</version>
      </dependency>
      <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-rt</artifactId>
        <version>${com.sun.xml.ws.jaxws-rt.version}</version>
        <exclusions>
          <exclusion>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>wstx-asl</artifactId>
          </exclusion>
          <exclusion>
            <groupId>javax.xml.stream</groupId>
            <artifactId>stax-api</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.3</version>
      </dependency>
      <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.6</version>
      </dependency>
      <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.2</version>
      </dependency>
      <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
      </dependency>
      <dependency>
        <groupId>commons-digester</groupId>
        <artifactId>commons-digester</artifactId>
        <version>2.0</version>
      </dependency>
      <dependency>
        <groupId>commons-el</groupId>
        <artifactId>commons-el</artifactId>
        <version>1.0</version>
      </dependency>
      <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1-atlassian-2</version>
      </dependency>
      <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
      </dependency>
      <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
      </dependency>
      <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
      </dependency>
      <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>2.0</version>
      </dependency>
      <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>1.6</version>
      </dependency>
      <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
      </dependency>
      <dependency>
        <groupId>edu.ucla.cs.compilers</groupId>
        <artifactId>jtb</artifactId>
        <version>1.3.2</version>
      </dependency>
      <dependency>
        <groupId>geronimo-spec</groupId>
        <artifactId>geronimo-spec-j2ee-management</artifactId>
        <version>1.0-rc4</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.7</version>
      </dependency>
      <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>${org.hsqldb.version}</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>javacc</groupId>
        <artifactId>javacc</artifactId>
        <version>4.1</version>
      </dependency>
      <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
      </dependency>
      <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>jsr250-api</artifactId>
        <version>1.0</version>
      </dependency>
      <dependency>
        <groupId>javax.batch</groupId>
        <artifactId>javax.batch-api</artifactId>
        <version>1.0</version>
      </dependency>
      <dependency>
        <groupId>javax.el</groupId>
        <artifactId>el-api</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>javax.el</groupId>
        <artifactId>el-impl</artifactId>
        <version>1.0</version>
      </dependency>
      <dependency>
        <groupId>javax.j2ee</groupId>
        <artifactId>j2ee</artifactId>
        <version>1.4</version>
      </dependency>
      <dependency>
        <groupId>javax.jee</groupId>
        <artifactId>javaee</artifactId>
        <version>5.0.5</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.1</version>
      </dependency>
      <dependency>
        <groupId>javax.naming</groupId>
        <artifactId>jndi</artifactId>
        <version>1.2.1</version>
      </dependency>
      <dependency>
        <groupId>javax.portlet</groupId>
        <artifactId>portlet-api</artifactId>
        <version>1.0</version>
      </dependency>
      <dependency>
        <groupId>javax.resource</groupId>
        <artifactId>connector</artifactId>
        <version>1.0</version>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${javax.servlet.servlet-api.version}</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
      </dependency>
      <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
      </dependency>

      <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0</version>
      </dependency>
      <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>policy</artifactId>
        <version>2.7.2</version>
      </dependency>
      <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jsr173_api</artifactId>
        <version>1.0</version>
      </dependency>
      <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
        <version>1.1.1</version>
        <exclusions>
          <exclusion>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
          </exclusion>
          <exclusion>
            <groupId>jdom</groupId>
            <artifactId>jdom</artifactId>
          </exclusion>
          <exclusion>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
          </exclusion>
          <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
          </exclusion>
          <exclusion>
            <groupId>xom</groupId>
            <artifactId>xom</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>jdom</groupId>
        <artifactId>jdom</artifactId>
        <version>1.1</version>
      </dependency>
      <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.3</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>junit-addons</groupId>
        <artifactId>junit-addons</artifactId>
        <version>1.4</version>
        <scope>test</scope>
      </dependency>


      <dependency>
        <groupId>net.sf.jt400</groupId>
        <artifactId>jt400-full</artifactId>
        <version>5.2</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>net.sourceforge.groboutils</groupId>
        <artifactId>GroboTestingJUnit-core</artifactId>
        <version>1.2.1</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr-runtime</artifactId>
        <version>${maven.antlr.version}</version>
      </dependency>
      <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr</artifactId>
        <version>${maven.antlr.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.1.1</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-exec</artifactId>
        <version>1.1</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.4.2</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.6</version>
      </dependency>
      <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.4.2.0</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.3</version>
      </dependency>
      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient-cache</artifactId>
        <version>4.5.3</version>
      </dependency>
      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.3</version>
      </dependency>
      <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-webdav</artifactId>
        <version>${maven.wagon-webdav.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-api</artifactId>
        <version>${maven.myfaces.version}</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-impl</artifactId>
        <version>${maven.myfaces.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.myfaces.tomahawk</groupId>
        <artifactId>tomahawk</artifactId>
        <version>1.1.6</version>
      </dependency>
      <dependency>
        <groupId>org.apache.myfaces.trinidad</groupId>
        <artifactId>trinidad-api</artifactId>
        <version>${maven.trinidad.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.myfaces.trinidad</groupId>
        <artifactId>trinidad-impl</artifactId>
        <version>${maven.trinidad.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache</groupId>
        <artifactId>poi</artifactId>
        <version>3.7</version>
      </dependency>
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${maven.aspectj.version}</version>
      </dependency>
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${maven.aspectj.version}</version>
      </dependency>
      <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>${org.codehaus.groovy.groovy-all.version}</version>
      </dependency>
      <dependency>
        <groupId>org.codehaus.groovy.modules.http-builder</groupId>
        <artifactId>http-builder</artifactId>
        <version>0.7.1</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.databene</groupId>
        <artifactId>databene-benerator</artifactId>
        <version>0.7.7</version>
      </dependency>
      <dependency>
        <groupId>org.dbunit</groupId>
        <artifactId>dbunit</artifactId>
        <version>2.4.7</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.1</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>${maven.eclipselink.version}</version>
      </dependency>
      <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>${maven.eclipselink.persistence.version}</version>
      </dependency>
      <dependency>
        <groupId>org.fusesource.hawtbuf</groupId>
        <artifactId>hawtbuf</artifactId>
        <version>1.11</version>
      </dependency>
      <dependency>
        <groupId>org.jvnet.jax-ws-commons.spring</groupId>
        <artifactId>jaxws-spring</artifactId>
        <version>1.9</version>
        <exclusions>
          <exclusion>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
          </exclusion>
          <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
          </exclusion>
          <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
          </exclusion>
          <exclusion>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
          </exclusion>
          <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
          </exclusion>
          <exclusion>
            <groupId>com.sun.xml.stream.buffer</groupId>
            <artifactId>streambuffer</artifactId>
          </exclusion>
          <exclusion>
            <groupId>org.jvnet.staxex</groupId>
            <artifactId>stax-ex</artifactId>
          </exclusion>
          <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
          </exclusion>
          <exclusion>
            <artifactId>servlet-api</artifactId>
            <groupId>javax.servlet</groupId>
          </exclusion>
          <exclusion>
            <artifactId>stax-api</artifactId>
            <groupId>javax.xml.stream</groupId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.jvnet.staxex</groupId>
        <artifactId>stax-ex</artifactId>
        <version>1.7.8</version>
      </dependency>
      <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>${org.spockframework.version}</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-spring</artifactId>
        <version>${org.spockframework.version}</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${org.springframework.spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${org.springframework.spring-boot.version}</version>
        <exclusions>
          <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
        <version>${org.springframework.spring-boot.version}</version>
        <exclusions>
          <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
        <version>${org.springframework.spring-boot.version}</version>
        <exclusions>
          <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-integration</artifactId>
        <version>${org.springframework.spring-boot.version}</version>
        <exclusions>
          <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>${org.springframework.spring-boot.version}</version>
        <scope>test</scope>
        <exclusions>
          <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>${org.apache.tomcat.embed.version}</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>${org.springframework.spring-boot.version}</version>
        <scope>provided</scope>
      </dependency>
  </dependencyManagement>

标签: javaspringspring-bootspring-security

解决方案


SecurityConfiguration只需在您的所有请求中禁用 Spring Security :

    @Override
    public void configure(WebSecurity web) {
        web
                .ignoring()
                .antMatchers("/**");
    }

或者,您可以对任何 HTTP 方法进行更精细的处理:

    @Override
    public void configure(WebSecurity web) {
        web
                .ignoring()
                .antMatchers(HttpMethod.OPTIONS)
                .antMatchers(HttpMethod.TRACE)
                .antMatchers(HttpMethod.PATCH)
                .antMatchers(HttpMethod.PUT)
                .antMatchers(HttpMethod.POST)
                .antMatchers(HttpMethod.GET);
    }

如需进一步微调,您还可以查阅有关此主题的文档


推荐阅读