gpt4 book ai didi

java - InMemoryUserDetailsManager updateUser 调用后未获取更新的密码

转载 作者:行者123 更新时间:2023-12-01 22:19:50 25 4
gpt4 key购买 nike

嗨,我有一个 Rest WS,使用 WebSecurityConfigurerAdapter 来实现 HTTP 基本身份验证。允许更新密码,我需要让 WS 获取更新后的密码,而无需重新启动服务器以下是代码:

安全配置

// init a user with credentials admin/password
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
//disable csrf
.csrf().disable()
//authentic all requests
.authorizeRequests().anyRequest().authenticated().and().httpBasic()
//disable session
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(inMemoryUserDetailsManager());
}

@Bean
public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
Properties users = new Properties();
users.put("admin", "password,USER,enabled");
return new InMemoryUserDetailsManager(users);
}
}

将更新密码的 Controller

@RestController
public class someController{
@Autowired
public InMemoryUserDetailsManager inMemoryUserDetailsManager;

// update password from password -> pass
@RequestMapping(...)
public updatePass(){
ArrayList<GrantedAuthority> grantedAuthoritiesList = new ArrayList<>();
grantedAuthoritiesList.add(new SimpleGrantedAuthority("USER"));

this.inMemoryUserDetailsManager.updateUser(new User("admin", "pass", grantedAuthoritiesList));
}

// another way that also doesn’t work
@RequestMapping(...)
public newUpdate(){
ArrayList<GrantedAuthority> grantedAuthoritiesList = new ArrayList<>();
grantedAuthoritiesList.add(new SimpleGrantedAuthority("USER"));
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("admin", "pass",
grantedAuthoritiesList);

SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(auth);
SecurityContextHolder.setContext(context);
}
}

第一次使用凭据 admin/password 调用 updatePass() 后,我可以看到调试器中的密码已更新为“pass”

enter image description here

我认为如果我再次调用 updatePass(),我应该使用 admin/pass。但事实证明,它仍在使用旧的管理员/密码

我在编写此代码时引用的来源 source1 source2

*我正在使用 Advance Rest Client 调用电话

最佳答案

当您更新密码时,如果用户经过身份验证,则必须在 springSecurityContext 对象中设置 UserDetails。

关于java - InMemoryUserDetailsManager updateUser 调用后未获取更新的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58600562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com