Advertisement
Guest User

Untitled

a guest
Jan 10th, 2016
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 KB | None | 0 0
  1. java.lang.IllegalArgumentException:
  2. Not an managed type: interface org.springframework.security.core.userdetails.UserDetails
  3.  
  4. @SpringBootApplication
  5. @Controller
  6. @EnableJpaRepositories(basePackages = "demo", considerNestedRepositories = true)
  7. public class UiApplication extends WebMvcConfigurerAdapter {
  8.  
  9. @Autowired
  10. private WebLeadRepository myrepo;
  11.  
  12. @Autowired
  13. private Users users;//duplicate from AuthenticationSecurity internal class below. Remove one?
  14.  
  15. // Match everything without a suffix (so not a static resource)
  16. @RequestMapping(value = "/{[path:[^\.]*}")
  17. public String redirect() {
  18. // Forward to home page so that route is preserved.
  19. return "forward:/";
  20. }
  21.  
  22. @RequestMapping("/user")
  23. @ResponseBody
  24. public Principal user(Principal user) {
  25. return user;
  26. }
  27.  
  28. //lots of other @RequestMapping @ResponseBody url handling methods
  29.  
  30. public static void main(String[] args) {
  31. SpringApplication.run(UiApplication.class, args);
  32. }
  33.  
  34. @Order(Ordered.HIGHEST_PRECEDENCE)
  35. @Configuration
  36. protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {
  37.  
  38. @Autowired
  39. private Users users;
  40.  
  41. @Override
  42. public void init(AuthenticationManagerBuilder auth) throws Exception {
  43. auth.userDetailsService(users);
  44. }
  45. }
  46.  
  47. @SuppressWarnings("deprecation")
  48. @Configuration
  49. @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
  50. @EnableWebMvcSecurity
  51. @EnableGlobalMethodSecurity(prePostEnabled = true)
  52. protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  53.  
  54. @Override
  55. protected void configure(HttpSecurity http) throws Exception {
  56. http.httpBasic().and().authorizeRequests()
  57. .antMatchers("/sign-up").permitAll()
  58. .antMatchers("/index.html", "/", "/login", "/something*")
  59. .permitAll().anyRequest().authenticated().and().csrf()
  60. .csrfTokenRepository(csrfTokenRepository()).and()
  61. .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
  62. }
  63.  
  64. private Filter csrfHeaderFilter() {
  65. return new OncePerRequestFilter() {
  66. @Override
  67. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  68. throws ServletException, IOException {
  69. CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
  70. if (csrf != null) {
  71. Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
  72. String token = csrf.getToken();
  73. if (cookie == null || token != null && !token.equals(cookie.getValue())) {
  74. cookie = new Cookie("XSRF-TOKEN", token);
  75. cookie.setPath("/");
  76. response.addCookie(cookie);
  77. }
  78. }
  79. filterChain.doFilter(request, response);
  80. }
  81. };
  82. }
  83.  
  84. private CsrfTokenRepository csrfTokenRepository() {
  85. HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
  86. repository.setHeaderName("X-XSRF-TOKEN");
  87. return repository;
  88. }
  89. }
  90.  
  91. @Repository//This repository is what Spring cannot seem to create in the stack trace
  92. interface UserRepository extends CrudRepository<UserDetails, Long> {
  93. User findByName(String name);
  94. }
  95.  
  96. }
  97.  
  98. @Service
  99. class Users implements UserDetailsManager {
  100.  
  101. private UserRepository repo;
  102.  
  103. @Autowired
  104. public Users(UserRepository repo) {this.repo = repo;}
  105.  
  106. @Override
  107. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  108. User user = repo.findByName(username);
  109. if (user == null) {throw new UsernameNotFoundException("Username was not found. ");}
  110. List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
  111. if (username.equals("admin")) {auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN");}
  112. String password = user.getPassword();
  113. return new org.springframework.security.core.userdetails.User(username, password, auth);
  114. }
  115.  
  116. @Override
  117. public void createUser(UserDetails user) {// TODO Auto-generated method stub
  118. repo.save(user);
  119. }
  120.  
  121. @Override
  122. public void updateUser(UserDetails user) {// TODO Auto-generated method stub
  123. repo.save(user);
  124. }
  125.  
  126. @Override
  127. public void deleteUser(String username) {// TODO Auto-generated method stub
  128. User deluser = (User)this.loadUserByUsername(username);
  129. repo.delete(deluser);
  130. }
  131.  
  132. @Override
  133. public void changePassword(String oldPassword, String newPassword) {
  134. // TODO Auto-generated method stub
  135. }
  136.  
  137. @Override
  138. public boolean userExists(String username) {
  139. // TODO Auto-generated method stub
  140. return false;
  141. }
  142.  
  143. }
  144.  
  145. @Entity
  146. class User implements UserDetails{
  147.  
  148. @GeneratedValue
  149. @Id
  150. private Long iduser;
  151. private String name;//valid email address only
  152. private String password;
  153. //lots of other properties that model all the things the User does in the app
  154.  
  155. //getters and setters
  156. public String getName() {return name;}//valid email address
  157. public void setName(String name) {this.name = name;}//valid email address
  158.  
  159. public String getPassword() {return password;}
  160. public void setPassword(String password) {this.password = password;}
  161.  
  162. //LOTS OF OTHER GETTERS AND SETTERS OMITTED HERE, THAT MANAGE MANY CUSTOM PROPERTIES
  163.  
  164. // Also, All the following are for implementing UserDetails
  165. @Override
  166. public Collection<? extends GrantedAuthority> getAuthorities() {// TODO Auto-generated method stub
  167. return null;
  168. }
  169. @Override
  170. public String getUsername() {// TODO Auto-generated method stub
  171. return null;
  172. }
  173. @Override
  174. public boolean isAccountNonExpired() {// TODO Auto-generated method stub
  175. return false;
  176. }
  177. @Override
  178. public boolean isAccountNonLocked() {// TODO Auto-generated method stub
  179. return false;
  180. }
  181. @Override
  182. public boolean isCredentialsNonExpired() {// TODO Auto-generated method stub
  183. return false;
  184. }
  185. @Override
  186. public boolean isEnabled() {// TODO Auto-generated method stub
  187. return false;
  188. }
  189. }
  190.  
  191. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'uiApplication.UserRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: interface org.springframework.security.core.userdetails.UserDetails
  192. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
  193. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
  194. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
  195. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
  196. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  197. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
  198. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
  199. at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
  200. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
  201. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
  202. at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
  203. at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
  204. ... 38 more
  205. Caused by: java.lang.IllegalArgumentException: Not an managed type: interface org.springframework.security.core.userdetails.UserDetails
  206. at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219)
  207. at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68)
  208. at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:67)
  209. at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:152)
  210. at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:99)
  211. at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81)
  212. at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185)
  213. at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)
  214. at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)
  215. at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
  216. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
  217. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement