カスタムログインページを作成することができ、私は春のセキュリティに新しいですし、春のセキュリティは、デフォルトのログイン・ページのレンダリングもなく、
WebSecurityConfigurerAdapter
Implementaion-
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder registry) throws Exception {
registry.userDetailsService(customUserDetailsService);
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/login","/login/form**","/register","/logout").permitAll()
.antMatchers("/admin","/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login/form")
.loginProcessingUrl("/login")
.failureUrl("/login/form?error")
.permitAll();
}
}
UserDetailsService
Implemtation
@Component
public class CustomUserDetailsService implements UserDetailsService
{
@Autowired
private UserService userService;
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
User user = userService.findUserByEmail(userName);
if(user == null){
throw new UsernameNotFoundException("UserName "+userName+" not found");
}
return new SecurityUser(user);
}
}
User
実装-
public class SecurityUser extends User implements UserDetails
{
private static final long serialVersionUID = 1L;
public SecurityUser(User user) {
if(user != null)
{
this.setId(user.getId());
this.setName(user.getName());
this.setEmail(user.getEmail());
this.setPassword(user.getPassword());
this.setDob(user.getDob());
this.setRoles(user.getRoles());
}
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>();
List<Role> userRoles = this.getRoles();
if(userRoles != null)
{
for (Role role : userRoles) {
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getRoleName());
authorities.add(authority);
}
}
return authorities;
}
@Override
public String getPassword() {
return super.getPassword();
}
@Override
public String getUsername() {
return super.getEmail();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
そして、私のSpringWebAppInitializer
クラスは -
public class SpringWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getServletConfigClasses()
{
return new Class<?>[] { AppConfig.class };
}
@Override
protected String[] getServletMappings()
{
return new String[] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses()
{
return new Class<?>[] { AppConfig.class};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[]{
new DelegatingFilterProxy("springSecurityFilterChain"),
new OpenEntityManagerInViewFilter()};
}
}
私の問題はある - ??どのようにセキュリティを春にこのユーザーを登録することができますはい、そのURLがどのような場合には、このための任意のデフォルトコントローラはありますか?代わりに、コントローラの
<form:form id="loginForm" method="post" action="${appUrl}/controller/login"
modelAttribute="user" class="register" role="form">
<div class="form-group">
<div class="">
<input type="text" id="username" name="username"
class="form-control register-input" placeholder="UserName" />
</div>
</div>
<div class="form-group">
<div class="">
<input type="password" id="password" name="password"
class="form-control register-input" placeholder="Password" />
</div>
</div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<div class="form-group">
<div class="col-sm-offset-3">
<input type="submit" class="btn btn-primary" value="Login">
</div>
</div>
</form:form>
いいえありません...春のセキュリティは、ユーザー管理ではなく、アプリケーションのセキュリティ保護に関するものです。 –
@ M.Deinumどのように私は春のセキュリティ –
に私たちのユーザーを登録することができます私はどのようなコードは、私のコントローラでは、このユーザーが現在ログインしている私のユーザーを確認するために書く必要があります紛失している –