2016-10-07 17 views
0

私はこのクラスを持っている:SpringMVC UserDetailsS​​erviceログイン

@Service 
public class AppUserDetailsService implements UserDetailsService { 

@Autowired 
private UserRepository userRepository; 

@Override 
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { 
    Optional<User> optionalUser = userRepository.findByEmailAndStatus(email, UserStatusEnum.ACTIVE); 
    User user = optionalUser.orElseThrow(() -> new UsernameNotFoundException("Usuário e/ou senha incorretos")); 
    return new UserSystem(user, getPermissoes(user)); 
} 

private Collection<? extends GrantedAuthority> getPermissoes(User user) { 
    Set<SimpleGrantedAuthority> authorities = new HashSet<>(); 

    user.getPermitionList().forEach(p -> authorities.add(new SimpleGrantedAuthority(p.getCode().getDescription().toUpperCase()))); 

    return authorities; 
    } 
} 

私のHTMLフォーム:

<form name="login" method="POST" th:action="@{/login}"> 
      <div class="form-group"> 
      <input type="email" class="form-control input-lg" id="email" placeholder="Email" name="email" 
       required="required"/> 
      </div> 
      <div class="form-group"> 
      <input type="password" class="form-control input-lg" id="password" placeholder="Senha" name="password" 
       required="required"/> 
      </div> 
      <div class="btn-group btn-group-justified" role="group"> 
      <div class="btn-group" role="group"> 
       <button type="button" class="btn btn-danger" onclick="showUserForm()">Cadastar</button> 
      </div> 
      <div class="btn-group" role="group"> 
       <button type="submit" class="btn btn-default">Entrar</button> 
      </div> 
      </div> 
      <button type="button" class="btn btn-link wimoo-link">Esqueceu sua senha?</button> 
    </form> 

マイログイン豆:

@GetMapping("/login") 
public ModelAndView login(@AuthenticationPrincipal User user) { 
    ModelAndView mv = new ModelAndView("login"); 
    mv.addObject(new User()); 

    if (user != null) { 
     mv = new ModelAndView("masterdata/home"); 
     return mv; 
    } 

    return mv; 
} 

問題は、私の方法では、ユーザーは 'パブリックModelAndViewログイン(@AuthenticationPrincipalユーザーユーザー) 'は常にnullです。メソッドloadUserByUsernameはokですが、Beanではユーザは常にnullです。

+0

おそらくそれは役に立ちます:https://stackoverflow.com/questions/32686587/null-authenticationprincipal-with-spring-boot-security – Jireugi

答えて

1

いくつか不足しています。ログインメソッドでフォームの詳細をユーザーにバインドしていません。あなたが持っているフォーム上

<div class="form-group"> 
    <input type="email" class="form-control input-lg" id="email" placeholder="Email" name="email" required="required"/> 
</div> 

あなたはユーザーオブジェクト上の、そして場合、ユーザーに電子メールフィールドにフォームからこのメールをバインドしたい場合は結合を行うために、これを行いますあなたが持っているクラス:

public class User{ 
    private String myEmailAddress; 
} 

は、あなたのinputタグに、あなたはあなたのログイン方法であなたのUserオブジェクトにフォームからその値をバインドするために、myEmailAddressに等しいname属性を追加する必要があります。

<div class="form-group"> 
    <input type="email" name="myEmailAddress" class="form-control input-lg" id="email" placeholder="Email" required="required"/> 
</div> 

あなたが今やらなければならないことは、実際にこの結合を行うために、あなたのログイン方法に@ModelAttribute注釈を追加することです。このように:

@GetMapping("/login") 
public ModelAndView login(@AuthenticationPrincipal @ModelAttribute User user) { 
    ModelAndView mv = new ModelAndView("login"); 
    mv.addObject(new User()); 

    if (user != null) { 
     mv = new ModelAndView("masterdata/home"); 
     return mv; 
    } 

    return mv; 
} 
関連する問題