3 回答

TA贡献1765条经验 获得超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` folder

TA贡献1818条经验 获得超3个赞
你真的不应该以这种方式来保护你的安全。通过 http 以明文形式发送密码不是好的做法。
这里有一些使用 spring security 进行基本身份验证的示例。
https://www.baeldung.com/spring-security-basic-authentication
https://www.baeldung.com/securing-a-restful-web-service-with-spring-security
如果您按照本教程进行操作,那么您可以做的是为初学者分配一个内存用户。然后您可以将您的身份验证详细信息进行 Base64Encode 以提供给用户。然后,对于每个用户,您可以发送身份验证详细信息,并且当用户名和密码通过网络时,没有人可以窥探用户名和密码,并且您的请求在到达您的控制器之前得到处理。这样,您就可以将业务逻辑与身份验证分离。
这至少是一个开始。希望这可以帮助。

TA贡献1848条经验 获得超6个赞
你不应该像这样处理授权。您可以做的是启用 spring-security 并使用 spring security 注释来处理这种情况。使用以下依赖项并设置基本的 spring 安全设置。
@PreAuthorize()然后可以使用诸如此类的注释在执行方法之前验证用户权限。如果您坚持,您甚至可以将此注释添加到控制器方法以在为每个请求提供服务之前进行验证。
您可以设置和LDAP 服务器或 Oauth 甚至使用数据库进行身份验证(如果您正在处理演示或其他内容)。
<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 中的这个示例项目提供了一个基本设置,您可以在其中使用 spring security:
https://github.com/mohchi/spring-security-request-mapping
参考使用: https ://www.mkyong.com/spring-security/spring-security-form-login-using-database/
添加回答
举报