[英]How do I create a custom JSON 401 page for my Spring MVC/Security restful web service?
[英]How can I validate the credentials in my database using Spring security and Restful web services?
我对Spring安全性很新。 我想用Basic Authentication和RestFul Webservices创建一个Spring安全应用程序。 我也在使用Spring Data JPA。
我通过互联网浏览了一些例子。 他们中的大多数都在spring应用程序本身中有一个Login UI表单。 此外,它不会验证数据库中的凭据。
但我的情况:我有一个单独的UI应用程序,从我的客户端应用程序我将发送一个登录URL(Restful api)调用。 我想从我的Spring应用程序中使用请求,并且应该从数据库验证凭据。
配置Spring Security
以对数据库上的凭据进行身份验证的简单方法是,您可以使用jdbcAuthentication()
方法。 所需的最小配置如下:
@Autowired
DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource);
}
您必须配置的唯一内容是DataSource,以便它能够访问您的数据库。
此最小配置将适用于有关数据库模式的一些假设。 它期望存在用户数据的某些表。
更具体地说,Spring Security内部的以下代码片段显示了
查找用户详细信息时将执行的SQL查询:
public static final String DEF_USERS_BY_USERNAME_QUERY = "select username,password,enabled "
+ "from users " + "where username = ?";
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "select username,authority "
+ "from authorities " + "where username = ?";
public static final String DEF_GROUP_AUTHORITIES_BY_USERNAME_QUERY = "select g.id, g.group_name, ga.authority "
+ "from groups g, group_members gm, group_authorities ga "
+ "where gm.username = ? " + "and g.id = ga.group_id "
+ "and g.id = gm.group_id";
第一个查询检索用户的用户名,密码以及是否启用了它们。 此信息用于验证用户。 下一个查询会查找用户授予的权限以进行授权,最终查询会查找授予用户作为组成员的权限。
如果您不想坚持Spring Security强制执行的默认数据库结构,您可以执行此操作
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username, password, true from <tbl_name> where username=?")
.authoritiesByUsernameQuery("select username, 'ROLE_ADMIN' from <tbl_name> where username=?");
}
在这种情况下,您只是覆盖身份验证和基本授权查询。 但您也可以通过使用自定义查询调用groupAuthoritiesByUsername()来覆盖组权限查询。
覆盖默认架构时,您需要遵循查询的基本合同。 这里需要注意的是,这些查询仅接受用户名作为参数。 权限查询选择包含用户名和授予权限的零行或多行。 并且组权限查询选择零行或多行,每行包含组ID,组名和权限。
您需要实现UserDetailsService接口。
您需要做的就是实现loadUserByUsername()
方法以查找给定用户用户名的用户。 然后, loadUserByUsername()
返回表示给定用户的UserDetails对象。
基本实施
public class SomeUserService implements UserDetailsService {
private final SomeRepository someRepository;
public SomeUserService(SomeRepository someRepository) {
this.someRepository = someRepository;
}
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
UserClass userObject = someRepository.findByUsername(username);
if (userObject != null) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
return new User(
userObject.getUsername(),
userObject.getPassword(),
authorities
);
}
throw new UsernameNotFoundException(
"User '" + username + "' not found.");
}
}
然后
@Autowired
SomeRepository someRepository;
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth
.userDetailsService(new SomeUserService(someRepository));
}
查看本文,它介绍了如何使用REST端点执行登录: http : //www.baeldung.com/securing-a-restful-web-service-with-spring-security
您可以通过修改身份验证提供程序来使用数据库中的凭据:
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username,password, enabled from users where username=?"
authorities-by-username-query="select username, role from user_roles where username =?"/>
</authentication-provider>
或者,正如Obi Wan - PallavJha所写,实现UserDetailService并使用它插入
<authentication-provider user-service-ref="YourUserDetailService" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.