我需要訪問我用 Spring Boot 創建的自己的主頁。我創建了一個回傳 HTML 頁面的簡單控制器
@Controller
public class HomePage{
public String home(){
return "home";
}
}
home.html 位于resources/templates/.
在我執行專案并進入后localhost:8080,出現以下頁面:

日志:
Using generated security password: a4752144-65d7-456c-b250-97e58d4fffcd
This generated password is for development use only. Your security configuration must be updated before running your application in production.
2022-08-29 15:19:17.389 INFO 19260 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@2adce412, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@629cf53c, org.springframework.security.web.context.SecurityContextPersistenceFilter@1fc386f8, org.springframework.security.web.header.HeaderWriterFilter@d7c4fcb, org.springframework.security.web.csrf.CsrfFilter@72d7afff, org.springframework.security.web.authentication.logout.LogoutFilter@2cd31214, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@5857d723, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@78ab63b5, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@6d84ab90, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@362fd4e9, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@3777fc44, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@79454d8e, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@3182143c, org.springframework.security.web.session.SessionManagementFilter@498503cb, org.springframework.security.web.access.ExceptionTranslationFilter@6d38a81d, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@294bb6ae] 2022-08-29 15:19:17.543 INFO 19260 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-08-29 15:19:17.553 INFO 19260
--- [ main] com...Application : Started DeardiaryApplication in 8.385 seconds (JVM running for 9.464)
首先 - 我不能使用提供的密碼:用戶名admin都root與此密碼不匹配,并且我收到錯誤的憑據錯誤,我還嘗試了 admin-tomcat,both-tomcat 用戶名-密碼對,正如我在教程中找到的那樣,但是沒有什么不匹配的。
但主要問題是我當然不需要使用任何密碼來訪問我自己的頁面。早期我使用spring-boot開發了一些專案,但我不記得我遇到過這樣的問題。我沒有通過句柄安裝任何 tomcat 服務器,我只是通過 build.gradle 將它們包含在我的專案中:
plugins {
id 'org.springframework.boot' version '2.7.3'
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
}
group = 'com.hltr'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.30'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
tasks.named('test') {
useJUnitPlatform()
}
那么,如何避免 Apache Tomcat 登錄頁面并訪問我的專案?
uj5u.com熱心網友回復:
這不是默認的 Apache 登錄頁面。它是 Spring Security 的默認登錄頁面(基本身份驗證)。
專注于
Using generated security password: a4752144-65d7-456c-b250-97e58d4fffcd
通過用戶名登錄:admin
密碼:(a4752144-65d7-456c-b250-97e58d4fffcd這是一個 UUID 版本 4 字串)
如果您不想看到此頁面,請通過洗掉此行來洗掉 Spring Security 的依賴項
implementation 'org.springframework.boot:spring-boot-starter-security'
如果您想根據自己的選擇設定默認用戶名和密碼對(帳戶),在檔案application.properties中,添加這樣的內容
spring.security.user.name=valentyn_hruzytskyi
spring.security.user.password=MyScrET42
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/504802.html
上一篇:Tomcat后臺處理
