@AuthenticationPrincipal with Spring Boot not working
<p>Using Spring Boot 1.3.1, I am having problems with <code>@AuthenticationPrincipal</code>.</p>

<p>This is my controller:</p>

<pre><code>@RestController
@RequestMapping("/api/user")
public class UserController {

    @RequestMapping("/")
    public UserDto user(@AuthenticationPrincipal(errorOnInvalidType = true) User user) {
        return UserDto.fromUser(user);
    }
}
</code></pre>

<p>This is my custom User class:</p>

<pre><code>@Entity()
@Table(name = "app_user")
public class User extends AbstractEntity&lt;UserId&gt; implements Serializable {
// ------------------------------ FIELDS ------------------------------

    @NotNull
    @Column(unique = true)
    @Pattern(regexp = "[a-zA-Z_\\-\\.0-9]+")
    @Size(min = 1, max = 20)
    private String username;
    private String password;
    @Column(unique = true)
    @Email
    private String emailAddress;
    @Enumerated(EnumType.STRING)
    @NotNull
    private UserRole role;
}
</code></pre>

<p>I also created a class to confirm to the <code>UserDetails</code> interface of Spring Security:</p>

<pre><code>public class CustomUserDetails extends User implements UserDetails {

    public CustomUserDetails(User user) {
        super(user);
    }

    @Override
    public Collection&lt;? extends GrantedAuthority&gt; getAuthorities() {
        return Sets.newHashSet(new SimpleGrantedAuthority("ROLE_" + getRole().name()));
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
</code></pre>

<p>Then in my <code>UserDetailsService</code>:</p>

<pre><code>@Override
public CustomUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    com.company.app.domain.account.User user = userRepository.findByUsername(username);

    if (user != null) {
        return new CustomUserDetails(user);
    } else {
        throw new UsernameNotFoundException(format("User %s does not exist!", username));
    }
}
</code></pre>

<p>I have an integration test that works perfectly. However, running the application itself (from IntelliJ IDEA), does not work. I get an exception:</p>

<pre><code>"CustomUserDetails{id=UserId{id=401868de-99ff-4bae-bcb6-225e3062ed33}} is not assignable to class com.company.app.domain.account.User"
</code></pre>

<p>But this is not true, since <code>CustomUserDetails</code> is a subclass of my custom <code>User</code> class.</p>

<p>Checking with the debugger, I see that this code in <code>AuthenticationPrincipalArgumentResolver</code> fails:</p>

<pre><code>if (principal != null
            &amp;&amp; !parameter.getParameterType().isAssignableFrom(principal.getClass())) {
</code></pre>

<p>The classloader of <code>parameter.getParameterType()</code> is an instance of <code>RestartClassloader</code>, while <code>principal.getClass()</code> has a classloader that is an instance of <code>Launcher$AppClassLoader</code>.</p>

<p>Is this a bug in Spring Boot or Spring Security or am I doing something wrong?</p>

<p><em>UPDATE:</em></p>

<p>I can confirm that disabling devtools of Spring Boot makes it work:</p>

<pre><code>public static void main(String[] args) {
    System.setProperty("spring.devtools.restart.enabled", "false");
    SpringApplication.run(MyApplication.class, args);
}
</code></pre>