2016-11-09 12 views
2

springとhibernateを使用して新しいユーザーを追加する方法について質問があります。私は認証に春のセキュリティを使用しており、oracle dbを使用して構成することができました。私が実装したいのは、新しいユーザーを登録することです。この場合の通常のアプローチは何ですか?私はいくつかの資料を読みましたが、それらのほとんどはjspで設計され実装されていました。クライアント側はangularjsで書かれています。 私がこれまで持っていることは、私はまた、いくつかのサービスが必要と考えている2つのエンドポイント/user/register新規ユーザーをspringとhibernateで登録する

@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エンティティクラスです:

@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; 
} 

@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を作成することは適切ではないと思います。あなたの答えに多くの感謝!

+0

データベースにプレーンテキストとしてパスワードを保存していないと教えてください。 – bradimus

+0

あなたは、春のセキュリティで既存のユーザーをどのように認証しているか明確には言及していません。どのようにあなたはちょうどangularjsとurページを設計しています。どのようにクライアントとサーバー間で通信していますか。あなたが提供した情報は少し不十分です – Acewin

+1

彼らはプレイテキストとして格納されていません。私はBCryptPasswordEncoder – skywalker

答えて

0

セキュリティ設定が簡単すぎます。あなたはそれにもっと多くの地獄を加えることができます。たとえば、役割、デフォルトのアクセスページ、エラーページなどを追加できます。投稿を使用したユーザーを登録すると、データを送信する必要があるため、良いことがわかります。しかし、ポストが非冪等であることを意味するのは、コントローラーの登録メソッドで、リダイレクトを使用する必要があることを繰り返すのは安全ではないことを意味します:これにスプリングセキュリティを使用する予定がない場合は、登録後/ your_desired_pa​​ge詳細はGETとPOSTの違いを参照してください)。角度を使用してjsonを返す必要があるため、返品データとエラー処理の自由度を高めるUserDetailsS​​ervice(hereを参照)の代わりにカスタム認証プロバイダを使用することをお勧めします。

関連する問題