有没有办法使用spring-oauth2添加登录成功处理程序?
我尝试使用基本身份验证筛选器,但它只筛选客户端凭据而不是用户凭据.
或者我是否需要创建自定义用户身份验证管理器?
TIA
我正在使用oath2 spring security.它的工作正常,除了登录成功和失败处理程序.就像在春季Web安全性中一样,OAth2没有明确定义的成功和失败处理程序挂钩来更新数据库并相应地设置响应.如果有人能为我提供相同的代码行,我将不胜感激.什么过滤器需要扩展及其在弹簧安全过滤器链中的位置
你能帮我解决我的问题吗?我使用 Spring OAuth2 为我的客户端生成了 JWT。我已经实现了一个授权和资源服务器以及一些网络安全配置,一切都通过在线指南完成。
它工作正常,但现在我想编辑有效负载并添加自定义属性,例如用户的名字和姓氏等。你能检查我的代码并告诉我我应该怎么做才能将其他属性添加到有效负载中吗?谢谢。
这是我的实现:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${security.signing-key}")
private String signingKey;
@Value("${security.encoding-strength}")
private Integer encodingStrength;
@Value("${security.security-realm}")
private String securityRealm;
@Autowired
private UserDetailsService userDetailsService;
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.httpBasic()
.realmName(securityRealm)
.and()
.csrf()
.disable();
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = …Run Code Online (Sandbox Code Playgroud) 我制作了一个简单的应用程序,它使用带有 oauth/jwt 提供程序的 spring 安全性。我通过自定义 JwtAccessTokenConverter 在 jwt 令牌中添加了额外的信息,并且效果很好。
我的问题是如何在我的 Rest Controller 中获取这些额外信息。
这是我的测试:
@RequestMapping(value = "/test", produces = { "application/json" },method = RequestMethod.GET)
public String testMethod(OAuth2Authentication authentication,
OAuth2AccessToken token,
Principal user){
.....
Object a=token.getAdditionalInformation();
Object b=token.getValue();
...
}
Run Code Online (Sandbox Code Playgroud)
结果是:
一些额外的信息:
我以为我的授权实现已经完成,但是当尝试检索UserDetails对象时,我得到的只是用户名。
我正在使用具有以下详细信息的oauth。
配置AuthenticationManager:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
Run Code Online (Sandbox Code Playgroud)
完成此操作后,我可以调试到userDetailsService中:
@Service
public class UserServiceImpl implements UserService, UserDetailsService {
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
MyUser persistedUser = userRepository.findByEmail(email);
if (persistedUser == null) {
throw new UsernameNotFoundException(String.format("The email %s doesn't exist", email));
}
List<GrantedAuthority> authorities = new ArrayList<>();
MyUser inMemoryUser = new MyUser(persistedUser.getEmail(), null, persistedUser.getEnabled(), false,
false, false, authorities);
return inMemoryUser;
}
}
Run Code Online (Sandbox Code Playgroud)
这样就很好了,我的客户又回来了JWT。但是我在调试以后的控制器方法时发现了以下问题。
@GetMapping
public @ResponseBody Iterable<Curriculum> getMyCurriculums(@AuthenticationPrincipal MyUser injectedUser) {
Authentication auth = …Run Code Online (Sandbox Code Playgroud)