JavaSpring是实现用户身份验证的常用方法
我是Java新手,我试图用Spring框架编写第一个web应用程序(我对django/python和sinatra/ruby有一些经验)。JavaSpring是实现用户身份验证的常用方法,java,spring,spring-mvc,Java,Spring,Spring Mvc,我是Java新手,我试图用Spring框架编写第一个web应用程序(我对django/python和sinatra/ruby有一些经验)。 我需要实现用户身份验证系统,但不知道正确实现这一点的最佳方法是什么。我应该为此使用Spring Security吗?或者还有其他方法吗?既然您启动了一个新的应用程序,我强烈建议您使用。在配置Spring的各个方面时,它将消除您最初的许多痛苦 至于安全性,您应该使用springsecurity,它当然很容易用springboot进行配置 根据指南,使用内存内身
我需要实现用户身份验证系统,但不知道正确实现这一点的最佳方法是什么。我应该为此使用Spring Security吗?或者还有其他方法吗?既然您启动了一个新的应用程序,我强烈建议您使用。在配置Spring的各个方面时,它将消除您最初的许多痛苦 至于安全性,您应该使用springsecurity,它当然很容易用springboot进行配置 根据指南,使用内存内身份验证的简单Spring安全配置如下所示:
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
如您所见,第一种方法用于配置应用程序的哪些部分需要保护以及如何保护,而第二种方法用于配置如何对用户进行身份验证
提供自定义的用户身份验证方法的一种常见方法是按原样提供自定义的UserDetailsService
实现
当您已经配置了数据源且用户凭据存储在其中时,配置身份验证的一种非常简单的方法如下:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource)
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN").and()
.withDefaultSchema();
}
}
是Spring Security的完整参考。这也是Spring引导文档的安全部分。您可以找到大量使用Spring安全性各个方面的示例应用程序
起初,Spring安全性看起来很复杂,但一旦掌握了窍门,您就会欣赏它的扩展功能集和可定制性
最后一点要注意的是,当涉及到许多应用程序中常见的安全性之类的事情时,没有必要重新发明轮子!使用Spring安全性(或者可能)因为您启动了一个新的应用程序,我强烈建议您使用。在配置Spring的各个方面时,它将消除您最初的许多痛苦
至于安全性,您应该使用springsecurity,它当然很容易用springboot进行配置
根据指南,使用内存内身份验证的简单Spring安全配置如下所示:
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
如您所见,第一种方法用于配置应用程序的哪些部分需要保护以及如何保护,而第二种方法用于配置如何对用户进行身份验证
提供自定义的用户身份验证方法的一种常见方法是按原样提供自定义的UserDetailsService
实现
当您已经配置了数据源且用户凭据存储在其中时,配置身份验证的一种非常简单的方法如下:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource)
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN").and()
.withDefaultSchema();
}
}
是Spring Security的完整参考。这也是Spring引导文档的安全部分。您可以找到大量使用Spring安全性各个方面的示例应用程序
起初,Spring安全性看起来很复杂,但一旦掌握了窍门,您就会欣赏它的扩展功能集和可定制性
最后一点要注意的是,当涉及到许多应用程序中常见的安全性之类的事情时,没有必要重新发明轮子!使用Spring Security(或者可能)感谢您的详细回复!:)感谢您如此详细的回复!:)