问题标签 [userdetailsservice]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
6059 浏览

spring-boot - Spring Boot 2+ 无法自动装配。'UserDetailsS​​ervice' 的 bean 不止一个

大家好,我是 spring security 和 jwt 的新手。我在我的 Spring Boot 项目中实现 Jwt 以保护用户登录,我使用的是 Spring Boot 2.1.5,我对 Spring Boot 2+ 中的新 bean 限制了解不多。我需要一些帮助..在这里我正在尝试@Autowired UserDetailsS​​ervice 并且代码运行良好..结果也很好..但intellij显示错误

@Autowired UserDetailsS​​ervice jwtUserDetailsS​​ervice

说...无法自动装配。UserDetailsS​​ervice 类型的 bean 不止一种。

谁能解释一下这里发生了什么错误,为什么我不能自动装配,为什么以及 spring boot 2+ 中的 Autowired 限制是什么?

并提前感谢

我的 customUserDetailService 是

我的 JwtController 类,它公开了重新结束点以生成 jwt 令牌

我的 JwtFilter 类

其他正常的东西,如实体、存储库和一些安全的重新发送点

0 投票
1 回答
218 浏览

java - 如何在 Spring Security 中将 PasswordEncoder 添加到 JdbcUserDetailsManager 中?

我正在学习 Spring Security,我想在 JdbcUserDetailsManager 中添加 BCryptPasswordEncoder。

这是代码:

我需要将 UserDetailsManager bean 注入其他类。谢谢!

0 投票
2 回答
739 浏览

java - How to correctly implement the UserDetailsService without having an username?

I am creating an oauth service in spring-boot and want to use user information from a MariaDB for authentication. When implementing UserDetailsService I have to override the function loadUserByUsername. The problem is that my user model does not have a field username, instead of this I want to load the users using their mail address. So my question: How do I correctly implement the UserDetailsService without having an username?

0 投票
2 回答
888 浏览

java - SpringBoot UserDetailsS​​ervice loadUserByUsername 方法从不使用自定义 WebSecurityConfigurerAdapter 调用

我正在关注此视频中的教程:https ://www.youtube.com/watch?v=X80nJ5T7YpE&list=PLqq-6Pq4lTTYTEooakHchTGglSvkZAjnE&index=12

我的代码处于 7:30 标记的状态。无论出于何种原因,我似乎无法让SecurityConfigurer.java该类识别我的自定义 UserDetailsS​​ervice 类,称为MyUserDetailsService.java.

预期的行为是,当我转到我在HelloResource.Java课堂上指定的 /hello 端点时,应该会生成一个登录页面。我应该能够使用该loadUserByUsername方法的硬编码返回用户值以及用户名和密码“foo”登录到网页。

不管我做了什么改变,似乎总是loadUserByUsername没有调用该方法(我已经用断点/打印语句证明了这一点)。这让我相信可能存在一些组件扫描问题,但我还没有看到任何工作!

我已经在这些链接上尝试过解决方案: OneTwoThree以及更多。这是我的相关文件:

JWTMain.java

HelloResource.java

MyUserDetailsS​​ervice.java

安全配置器.java

应用程序属性

构建.gradle

除此之外,我无法对正在发生的事情做出正面或反面。让我知道,谢谢。

0 投票
1 回答
681 浏览

spring-boot - 用户角色 Spring Data Rest Spring 安全性

我有一个我不太明白的错误。我没有找到任何解释为什么它不起作用的教程。我有这个使用spring security的spring boot应用程序。

当我发出这个 POST 请求时:http://localhost:8181/roles 正文:

它工作正常。

当我发出这个 POST 请求时:http://localhost:8181/users 正文:

它工作正常

但是当我发出这个 GET 请求时:http://localhost:8181/users 具有正确的凭据(用户名:用户,密码:pass)

它返回:

我不知道为什么它会返回 403。

PS:所有请求都在 Postman 上完成

UnoApplication.java

用户.java

角色.java

MyUserDetails.java

用户存储库.java

RoleRepository.java

MyUserDetailsS​​ervice.java

安全配置.java

感谢您的时间。

编辑

我在 application.properties 中添加了这一行:logging.level.org.springframework.security=DEBUG

当我发出之前的 GET 请求时,它在后端看起来像这样: Spring part 2 User Role

0 投票
1 回答
525 浏览

spring - 配置中的默认管理员用户和数据库中的其他用户

我正在使用带有 spring security 和 jpa 的 spring boot。我想在配置中有管理员用户,在数据库中有其他用户。问题是当我添加时UserDetailsService,管理员用户application.properties停止工作。

application.properties

Spring Security 配置类:

0 投票
3 回答
903 浏览

spring-boot - Spring Boot Oauth2 资源服务器 UserDetailsS​​ervice

试图让 UserDetailsS​​ervice 为我设置的 oauth2 资源服务器工作。我能够成功地对 jwt 进行身份验证,但我似乎没有做任何事情来让它调用 loadUserByUsername 方法。这最初是使用 SAML 并且可以正常工作,但是现在我已经切换到 Oauth2 并且无法正常工作。

我在 google 上发现我可以使用 @service 将类注册为一个 bean,spring 会选择它,但它不起作用。我也尝试通过 AuthenticationManagerBuilder 添加它,但这也不起作用。我的猜测是,它的 jwt 方面有它自己的 UserDetailsS​​ervice ,它已经实现并且优先于我的。也就是说,让我调用的正确方法是什么,或者在身份验证完成后以某种方式手动调用我的用户加载逻辑并覆盖 Principal 对象是否更好?我需要在调用端点之前执行此操作,以便 PreAuthorize 可以检查 UserDetailsS​​ervice 加载的角色。

0 投票
3 回答
986 浏览

spring-mvc - 使用 Postman 测试登录 api Spring Security

我正在为电子商务网站开发一个 Spring Boot 项目,作为初学者,我尝试在其中添加 Spring Security,所以问题是当我尝试使用邮递员测试我的休息登录 API 时,我的状态码为 200,并且body 始终是 spring security 的默认登录页面。我将感谢任何建议或任何解决方案。

这是我的用户类:

这是我的角色枚举:

MyUserDetails 类:

这是 MyUserDetailsS​​ervice :

这是我的 RestController :

这是我的安全配置类:

这是我在数据库中的用户(MySQL):

数据库中的用户

最后,这是 postman 中的测试结果:

在 Postman 中测试

0 投票
2 回答
493 浏览

spring - 如何在 authenticationManagerBuilder 上注册多个 UserDetailsS​​ervice

我有两个不同的普通用户和管理员存储库和单独的 url 端点进行身份验证。我希望身份验证管理器对端点单独使用UserDetailsService,因为普通用户和管理员用户可以拥有相同的用户名和密码,但存储库不同。

示例:如果端点命中是 user_login 然后是 UserDetailsS​​ervice1 如果端点命中是 admin_login 然后是 UserDetailsS​​ervice2 我怎样才能做到这一点?

0 投票
2 回答
368 浏览

java - UserDetailsS​​ervice 中的 UserRepository 为空

我正在使用 Spring 3.0.7,并通过UserDetailsServicespring-secrutiy.xml 创建了 bean,以将其用作用户的提供者

不幸的是, userRepository 是 null 我不知道为什么,所以我在对其对象执行操作时得到 NPE。

用户详情服务

在应用程序启动时,我将一些初始数据添加到 DB 中,因此必须创建 UserRepository 的 bean,但我不知道为什么它在 UserDetailsS​​erviceImpl 中被视为 null。

Spring-security.xml

你能告诉我这里有什么问题吗?