Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用spring和hibernate注册新用户_Java_Spring_Hibernate - Fatal编程技术网

Java 使用spring和hibernate注册新用户

Java 使用spring和hibernate注册新用户,java,spring,hibernate,Java,Spring,Hibernate,我有一个关于使用spring和hibernate添加新用户的问题。我使用spring security进行身份验证,并使用oracle db对其进行了配置。接下来我要实现的是注册新用户。在这种情况下,通常的做法是什么?我读了一些资料,但其中大部分是用jsp设计和实现的,我的客户端是用angularjs编写的。 到目前为止,我拥有的是一个具有两个端点的控制器/user和/register @Controller public class UserController { @Autowired p

我有一个关于使用spring和hibernate添加新用户的问题。我使用spring security进行身份验证,并使用oracle db对其进行了配置。接下来我要实现的是注册新用户。在这种情况下,通常的做法是什么?我读了一些资料,但其中大部分是用jsp设计和实现的,我的客户端是用angularjs编写的。 到目前为止,我拥有的是一个具有两个端点的控制器
/user
/register

@Controller
public class UserController {

@Autowired
private RegisterService registerService;

@RequestMapping("/user")
@ResponseBody
public Principal user(Principal user) {
    return user;
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
@ResponseBody
public void registerUser() {
    User user = new User();
    registerService.save(user);
}
}
我想我还需要一些服务。在我的例子中,这是
RegisterService
和该服务RegisterServiceImpl的实现:

@Service
public class RegisterServiceImpl implements RegisterService {

@Autowired
private UserDao userDao;

@Autowired
private SessionFactory sessionFactory;


@Transactional
@Override
public void save(User user) {
    // what should be the implementation of this method?

    Session session = sessionFactory.getCurrentSession();
    session.save(user);
}
}
这是我的
UserDao
实体类:

@Entity
@Table(name = "users")
public class User {

@Id
@Column(name = "id")
private int id;

@Column(name = "username")
private String userName;

@Column(name = "password")
private String password;

@Column(name = "email")
private String email;

@Column(name = "is_enabled")
private boolean isEnabled;

@ManyToOne
@JoinColumn(name = "department_id")
private Department department;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<UserRole> userRole = new HashSet<UserRole>(0);

public User() {

}

public User(String username, String password, Boolean isEnabled,             Set<UserRole> userRole) { 
    this.userName = username;
    this.password = password;
    this.isEnabled = isEnabled;
    this.userRole = userRole;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getUsername() {
    return userName;
}

public void setUsername(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public boolean isEnabled() {
    return isEnabled;
}

public void setEnabled(boolean isEnabled) {
    this.isEnabled = isEnabled;
}

public Department getDepartment() {
    return department;
}

public void setDepartment(Department department) {
    this.department = department;
}

public Set<UserRole> getUserRole() {
    return this.userRole;
}

public void setUserRole(Set<UserRole> userRole) {
    this.userRole = userRole;
}
}


关于用户注册,我想在
user
中设置所有类成员,但我不确定如何实现。我不认为为每个类成员创建
@PathVariable
是合适的,因为我会发送敏感数据用户名和密码。非常感谢你的回答

您的安全配置太简单。你可以给它添加更多。例如,您可以添加角色、默认访问页、错误页等。我可以看到,对于注册用户,您使用了post,这很好,因为您需要提交数据。但是,请记住post是非幂等的,这意味着在控制器中的register方法中重复是不安全的,因此,如果您不打算使用spring安全性,则在注册后至少需要使用redirect:/your_desired_页面(有关更多信息,请参阅GET和post之间的差异)。因为您使用angular并需要返回json,所以我建议您使用自定义身份验证提供程序而不是UserDetailsService(请参阅)这将使您在返回数据和错误处理方面有更多的自由。

请告诉我,您没有将密码以纯文本形式存储在数据库中。您没有明确提到如何使用spring security对现有用户进行身份验证。你是如何用angularjs设计你的页面的。以及客户端和服务器之间的通信方式。你提供的信息有点不足,它们没有存储为播放文本。我使用的是BCryptPasswordEncoderso,你能澄清一下你的spring安全实现吗?不,不要创建路径变量,使用表单post。您似乎对HTTP不太精通,因此这可能是一个学习机会,但POST(和PUT)请求的主体可以是JSON、x-www-urlencoded键值对、xml等。。。设计一个有效负载来传递消息并从中填充对象。查看Jackson的spring和json
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
UserDetailsService userDetailsService;

@Autowired
LogoutSuccess logoutSuccess;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}

@Override
protected void configure(HttpSecurity http) throws Exception {
  http
    .httpBasic()
  .and()
    .authorizeRequests()
      .antMatchers("/profile", "/logout", "/home").permitAll()
      .anyRequest().authenticated()
   .and()
   .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
            .csrf().disable();
}


@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/app/**");
}

private CsrfTokenRepository csrfTokenRepository() {
      HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
      repository.setHeaderName("X-XSRF-TOKEN");
      return repository;
    }

@Bean
public PasswordEncoder passwordEncoder() {
    PasswordEncoder encoder = new BCryptPasswordEncoder();
    return encoder;
}