Я создал простое приложение для входа в Spring Spring MVC.Оно работает.Но пока я вошел в систему и аутентифицировался с помощью role_user
, я хочу иметь возможность создать другого пользователя с role_company
.
Есть идеи, как мне это сделать?
Спасибо.Вот SecurityConfig, классы формы регистрации для регистрации пользователя и компании.
@Configuration
@EnableWebSecurity
открытый класс SecurityConfig расширяет WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
public SecurityConfig(@Qualifier(value = "userRepositoryUserDetailsService") UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/manager/**")
.hasRole("USER")
.antMatchers("/place-promoOrder", "/orders").hasRole("COMPANY")
.antMatchers("/", "/**").permitAll()
.and()
.formLogin()
//todo change url when company class and access page done
.loginPage("/login").defaultSuccessUrl("/")
.and()
.logout()
.logoutSuccessUrl("/")
.and()
.csrf().disable();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
public class RegistrationForm {
private String username;
private String password;
public User toUser(PasswordEncoder passwordEncoder){
return new User(username, passwordEncoder.encode(password));
}
}
public class CompanyRegistrationForm {
private String companyName;
private String companyEmail;
private Integer zkpo;
private String username;
private String password;
public Company toCompany(PasswordEncoder passwordEncoder){
return new Company(username, passwordEncoder.encode(password), companyName, companyEmail, zkpo);
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Promo MMplus</title>
</head>
<body>
<h1>Register</h1>
<img th:src="@{/images/mmplus.png}"/>
<form method="post" th:action="@{/register}" id="registerForm">
<label for="username">Username: </label>
<input type="text" name="username"/><br/>
<label for="password">Password: </label>
<input type="password" name="password"/><br/>
<label for="confirm">Confirm password: </label>
<input type="password" name="confirm"/><br/>
<input type="submit" value="Register"/>
</form>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Promo MMplus</title>
</head>
<body>
<h1>Register</h1>
<img th:src="@{/images/mmplus.png}"/>
<form method="post" th:action="@{/manager/registerCompany}" id="registerForm">
<label for="companyName">Company:</label>
<input type="text" name="companyName">
<label for="companyEmail">Email:</label>
<input type="text" name="companyEmail">
<label for="zkpo">ZKPO:</label>
<input type="number" name="zkpo">
<label for="username">Username: </label>
<input type="text" name="username"/><br/>
<label for="password">Password: </label>
<input type="password" name="password"/><br/>
<label for="confirm">Confirm password: </label>
<input type="password" name="confirm"/><br/>
<input type="submit" value="Register"/>
</form>
</body>
</html>