3 回答
TA貢獻1765條經(jīng)驗 獲得超5個贊
return ClassPathResource("static/main.html") should answer your question, don't forget to specify `static` folder at the beginning as `ClassPathResource` points to the `resources` folderTA貢獻1818條經(jīng)驗 獲得超3個贊
你真的不應該以這種方式來保護你的安全。通過 http 以明文形式發(fā)送密碼不是好的做法。
這里有一些使用 spring security 進行基本身份驗證的示例。
https://www.baeldung.com/spring-security-basic-authentication
https://www.baeldung.com/securing-a-restful-web-service-with-spring-security
如果您按照本教程進行操作,那么您可以做的是為初學者分配一個內(nèi)存用戶。然后您可以將您的身份驗證詳細信息進行 Base64Encode 以提供給用戶。然后,對于每個用戶,您可以發(fā)送身份驗證詳細信息,并且當用戶名和密碼通過網(wǎng)絡時,沒有人可以窺探用戶名和密碼,并且您的請求在到達您的控制器之前得到處理。這樣,您就可以將業(yè)務邏輯與身份驗證分離。
這至少是一個開始。希望這可以幫助。
TA貢獻1848條經(jīng)驗 獲得超6個贊
你不應該像這樣處理授權(quán)。您可以做的是啟用 spring-security 并使用 spring security 注釋來處理這種情況。使用以下依賴項并設(shè)置基本的 spring 安全設(shè)置。
@PreAuthorize()然后可以使用諸如此類的注釋在執(zhí)行方法之前驗證用戶權(quán)限。如果您堅持,您甚至可以將此注釋添加到控制器方法以在為每個請求提供服務之前進行驗證。
您可以設(shè)置和LDAP 服務器或 Oauth 甚至使用數(shù)據(jù)庫進行身份驗證(如果您正在處理演示或其他內(nèi)容)。
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version>
</dependency>
使用如下配置類來配置安全性:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery(
"select username,password, enabled from users where username=?")
.authoritiesByUsernameQuery(
"select username, role from user_roles where username=?");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") //To check admin role permission
.and()
.formLogin().loginPage("/login").failureUrl("/login?error") //provide failure url
.usernameParameter("username").passwordParameter("password")
.and()
.logout().logoutSuccessUrl("/login?logout")
.and()
.exceptionHandling().accessDeniedPage("/403")
.and()
.csrf();
}
}
github 中的這個示例項目提供了一個基本設(shè)置,您可以在其中使用 spring security:
https://github.com/mohchi/spring-security-request-mapping
參考使用: https ://www.mkyong.com/spring-security/spring-security-form-login-using-database/
添加回答
舉報
