Spring Security由注册用户注册另一个用户

时间:2019-07-09 20:55:42

标签: java spring-mvc spring-security

我已经创建了简单的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>

0 个答案:

没有答案
相关问题