我有两个表'用户'和'角色'。我想创建一个登录api(例如'/ login'),它将用户名和密码作为json数据。我想检查给定的凭证是否是有效的凭证,如果是,那么我想将用户设置为已认证的用户,以便他/她可以拥有受保护的资源。我是Spring引导框架的新手,我不知道该怎么做。我已阅读官方文档,但找不到任何资源。有人可以帮我解决这个问题吗?如何在春季开机时手动验证用户?
-2
A
回答
0
在Spring中你有很多选择来实现这种认证。
案例1: -如果您正在构建REST服务,那么您可以在以下几个方面实现安全性:
我) - 你可以使用基本的身份验证您的用户进行身份验证。
ii) - 您可以使用OAuth2来验证和授权您的用户。
案例2:如果您正在构建Web应用程序
我) - 您可以使用身份验证令牌(在单页应用SPA的情况下)
II) - 你可以使用基于会话的身份验证(传统登录表单和所有)
我猜你是初学者模式,所以我建议你首先通过登录表单了解Web应用程序中的控制流用户身份验证。所以我们来看看一些代码。
我假设你已经设置了一个基本的弹簧项目,现在你正在实现安全。
USER - 用户表的Hibernate实体; 作用 - Hibernate的实体为您的角色表
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthProvider customAuthProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
// everyone is allowed tp view login page
http.authorizeRequests().antMatchers("/login").permitAll().and();
http.authorizeRequests().antMatchers("custom_base_path" + "**").authenticated().and().
formLogin().loginPage("/loginForm).loginProcessingUrl("/loginUser")
.usernameParameter("username").passwordParameter("password")
.defaultSuccessUrl("custom_base_path+ "home", true);
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthProvider);
}
//CustomAuthProvider
@Component
public class CustomAuthentiationProvider implements AuthenticationProvider{
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String userid = authentication.getName();
String password = authentication.getCredentials().toString();
Authentication auth = null;
try {
//write your custom logic to match username, password
boolean userExists = your_method_that_checks_username_and_password
if(userExists){
List<Role> roleList= roleDao.getRoleList(userid);
if (roleList == null || roleList.isEmpty()) {
throw new NoRoleAssignedException("No roles is assigned to "+userid);
}
auth = new UsernamePasswordAuthenticationToken(userid, password,getGrantedAuthorities(roleList));
}
} catch (Exception e) {
log.error("error", e);
}
return auth;
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
public List<GrantedAuthority> getGrantedAuthorities(List<Role> roleList) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (Role role : roleList) {
authorities.add(new SimpleGrantedAuthority(role.getRoleName());
}
return authorities;
}
}
注:请考虑这些代码,了解认证的逻辑。不要考虑为完美的代码(不适用于生产环境)。你随时可以给我打电话,我会向你提供更多的建议。
+0
为什么没有投票?即使在接受答案后 –
相关问题
- 1. 如何在春季启动时验证用户?
- 2. 手动验证春季安全
- 3. 春季开机+春季安全:如何取消基本身份验证表格
- 4. 春季开机慢启动
- 5. 春季开机仅使用@valid形式验证特定属性
- 6. 如何在春季开机注册ServletContextListener
- 7. 如何在春季开机时使用运动衫2.0和运动衫mvc?
- 8. oAuth2春季令牌验证/验证
- 9. 春季启动基本身份验证
- 10. 春季启动基本身份验证的用户名未通过验证
- 11. 春季启动+春季LDAP +服务帐户+错误凭证
- 12. 春季开机如何编辑实体
- 13. 如何春季开机初始化库
- 14. 春季验证注释
- 15. JSR 303 Bean验证春季
- 16. 春季条件验证
- 17. 春季webflow输入验证
- 18. 春季休眠验证@RequestBody
- 19. 春季安全ldap验证
- 20. 春季开机自动刷新缓存
- 21. 春季开机休眠慢启动
- 22. 春季开机自动导入applicationContext.xml?
- 23. 手动验证用户
- 24. 春季开机基本版
- 25. 春季开机密码Bcrypt
- 26. 如何在春季启动使用@Autowired手动新实例
- 27. 在春季启动客户
- 28. 春季启动 - 测试 - 验证:验证为
- 29. 如何验证用户在春季安全中使用信息头信息
- 30. 春季验证:(portlet)问题,如noclassdeffounderror
使用基于弹簧安全角色的授权。 [此链接](http://www.baeldung.com/role-and-privilege-for-spring-security-registration)可能会在一定程度上帮助你 – harshavmb
@harshavmb非常感谢你。你的链接非常有帮助。 –