inMemory에 사용자를 추가하려면 어떻게 해야 합니까?구축 후 인증 빌더를 사용하시겠습니까?
어플리케이션의 최초 로드 시에 모든 사용자를 AuthenticationManagerBuilder에 로딩할 수 있었습니다만, 기동 후에 사용자를 추가할 필요가 있습니다.
기동:
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
...
auth.inMemoryAuthentication().withUser(email).password(password).roles(roles.toArray(new String[roles.size()])).and().passwordEncoder(encoder());
이것은 특정 시점에서는 매우 효과적이지만, 애플리케이션 실행 중에 사용자를 추가할 수 있는 사용 사례가 있습니다.
이 포스트스타트업(컨트롤러/서비스 경유)은 어떤 방법으로 실행할 수 있습니까?내 생각에 그건 아마도InMemoryUserDetailsManager(이것에는,createUser()method)를 참조하거나 설정하는 방법을 잘 모르겠습니다.
다음의 코드는, 고객의 요구에 응합니다.
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//whatever here
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(inMemoryUserDetailsManager());
}
@Bean
public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
final Properties users = new Properties();
users.put("user","pass,ROLE_USER,enabled"); //add whatever other user you need
return new InMemoryUserDetailsManager(users);
}
}
사용방법InMemoryUserDetailsManager사용자의 존재를 추가 및 확인하는 슈퍼 심플 컨트롤러 위에 설정한 경우 다음과 같습니다.
@RestController
@RequestMapping("user")
public class SimpleSecurityController {
private final InMemoryUserDetailsManager inMemoryUserDetailsManager;
@Autowired
public SimpleSecurityController(InMemoryUserDetailsManager inMemoryUserDetailsManager) {
this.inMemoryUserDetailsManager = inMemoryUserDetailsManager;
}
@RequestMapping("exists/{username}")
public boolean userExists(@PathVariable("username") String username ) {
return inMemoryUserDetailsManager.userExists(username);
}
@RequestMapping("add/{username}/{password}")
public String add(@PathVariable("username") String username, @PathVariable("password") String password) {
inMemoryUserDetailsManager.createUser(new User(username, password, new ArrayList<GrantedAuthority>()));
return "added";
}
}
또, Spring Boot의 자동 설정을 사용하고 있는 경우는, 다음을 추가할 필요가 있는 것에 주의해 주세요.
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
Spring Boot이 보안 자동 설정을 시도하지 않도록 합니다.
업데이트 @Adam Michalik의 설명대로@EnableWebMvcSecurity권장되지 않으므로 다음으로 대체해야 합니다.@EnableWebSecurity
이 코드를 변경했습니다.
inMemoryUserDetailsManager.createUser(new User(username, password, new ArrayList<GrantedAuthority>()));
@contend의 답변에서:
inMemoryUserDetailsManager.createUser(User.withUsername(username).password(password).roles("USER").build());
기본적으로 새 사용자에게 권한이 없기 때문입니다.
언급URL : https://stackoverflow.com/questions/25869260/how-can-i-add-users-to-the-inmemoryauthentication-builder-after-it-has-been-buil
'programing' 카테고리의 다른 글
| Spring Webflux, 정적 콘텐츠를 제공하기 위해 index.html로 전송하는 방법 (0) | 2023.04.05 |
|---|---|
| 오브젝트 어레이를 JSON 경유로 ASP에 투고합니다.넷 MVC3 (0) | 2023.04.05 |
| 반응에서 스크롤 애니메이션 처리 (0) | 2023.04.05 |
| angular js에 사용할 apache 2에 대한 규칙 다시 쓰기 (0) | 2023.04.05 |
| MERN에서 백엔드 및 프런트엔드의 파일 구조를 구성하는 방법 (0) | 2023.04.05 |