正在为我的卡路里管理API测试一个硬编码端点。我硬编码了一些用户在我的 UserService.java
并将它们映射在我的 UserController
至 /users
url。
随着Tomcat运行在 localhost:8080
我以为,当我去 localhost:8080/users
我将能够看到我添加的用户。
相反,它把我带到一个由spring创建的登录页面。尽管我可以用 "user "作为用户名登录,而且生成的密码也在构建文件中,但我不希望这样做,因为我将在未来做自己的认证。
当翻阅构建文件信息时,登录页面与这些信息是相通的。
2020-04-23 11:22:38.314 INFO 1548 --- [restartedMain] o.s.s.web.DefaultSecurityFilterChain : 创建过滤链:任何请求,[org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@150d0d25,org.springframework.security.web.context.SecurityContextPersistenceFilter@7c82be70,org.springframework.security.web.header.HeaderWriterFilter@3。 HeaderWriterFilter@3258c818,org.springframework.security.web.csrf.CsrfFilter@18dd2f3,org.springframework.security.web.authentication.logout.LogoutFilter@336c3f7a,org.springframework.security.web.authentication.UsernamePilter@3258c818。 security.web.authentication.UsernamePasswordAuthenticationFilter@641198db, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@7b7cd5a, org. springframework.security.web.savedrequest.RequestCacheAwareFilter@77927c43,org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@6861d187,org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@6861d187。 springframework.security.web.authenticationFilter@d43945e, org.springframework.security.web.session.SessionManagementFilter@19cc57e5, org.springframework.security.web.access.ExceptionTranslationFilter@7b984a77, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@2521604c] 。
我没有任何与安全或认证相关的依赖关系,这是否来自于有这些信息的其他依赖关系。我已经附上了我的pom.xl
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.M4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.MS3.bootcamp</groupId>
<artifactId>healthDiary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>healthDiary</name>
<description>Bootcamp project for MS3</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.gurux</groupId>
<artifactId>gurux.dlms</artifactId>
<version>4.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
你可以通过在你的应用程序上面添加注释来排除默认的弹簧安全配置。
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
@ServletComponentScan
public class Application {
}
正如你在问题中所说的,似乎Spring Security的行为是在依赖关系上,所以如果你处理的是Spring Security的http basic默认登录,正确的解决方法是像下面这样配置它。
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests().antMatchers("/**").permitAll()
.and();
}
}
这段代码 http.authorizeRequests().antMatchers("/**").permitAll()
在此上下文中,允许路由不需要认证。在这个上下文中,允许路由不需要认证。/**
告诉所有来自上下文的可能路由。默认情况下,Spring的所有路由都需要认证。因此,当你在没有安全配置的情况下将Spring Security放在项目上时,会强制所有用户登录。我想这是为了安全起见。
我做了一个项目,是一个简单的RESTful API与spring boot的例子,但我没有使用我在Github上发布的 "Spring Boot Data Rest",这个配置使用的类是 SecurityConfiguration.java储存库的完整内容在 画廊-弹簧靴.
更多关于Spring Security的信息,请访问 安全性与Spring