0

I have this class:

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

My html form:

<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>

My login Bean:

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

The problem is, the User in my method 'public ModelAndView login(@AuthenticationPrincipal User user)' is always null. The method loadUserByUsername is ok, but in my bean the user is always null.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Maybe that helps: https://stackoverflow.com/questions/32686587/null-authenticationprincipal-with-spring-boot-security – Jireugi Oct 07 '16 at 20:37

1 Answers1

1

You are missing a few things. You are not binding your form details to your user in the login method. In order to do the binding do this:

On your form you have:

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

if you want to bind this email from your form to the email field on your User object, then if on the User class you have:

public class User{
    private String myEmailAddress;
}

then on your input tag, you should add a name attribute that equals to myEmailAddress in order to bind that value from your form to your User object on your login method.

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

All you need to do now is to add the @ModelAttribute annotation to your login method in order to actually do this binding. Like this:

@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; 
}
Moshe Arad
  • 3,587
  • 4
  • 18
  • 33