Guest User

Untitled

a guest
Jan 5th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. package com.lixin.readinglist;
  2.  
  3. import org.springframework.data.annotation.Id;
  4. import org.springframework.security.core.GrantedAuthority;
  5. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  6. import org.springframework.security.core.userdetails.UserDetails;
  7.  
  8. import javax.persistence.Entity;
  9. import java.util.Collection;
  10. import java.util.Collections;
  11.  
  12. /**
  13. * @author lixin
  14. */
  15. @Entity
  16. public class Reader implements UserDetails {
  17.  
  18. private static final long serialVersionUID = 1L;
  19.  
  20. @Id
  21. private String username;
  22. private String fullname;
  23. private String password;
  24.  
  25. @Override
  26. public String getUsername() {
  27. return username;
  28. }
  29.  
  30. public void setUsername(String username) {
  31. this.username = username;
  32. }
  33.  
  34. public String getFullname() {
  35. return fullname;
  36. }
  37.  
  38. public void setFullname(String fullname) {
  39. this.fullname = fullname;
  40. }
  41.  
  42. @Override
  43. public String getPassword() {
  44. return password;
  45. }
  46.  
  47. public void setPassword(String password) {
  48. this.password = password;
  49. }
  50.  
  51. @Override
  52. public Collection<? extends GrantedAuthority> getAuthorities() {
  53. return Collections.singletonList(new SimpleGrantedAuthority("READER"));
  54. }
  55.  
  56. @Override
  57. public boolean isAccountNonExpired() {
  58. return true;
  59. }
  60.  
  61. @Override
  62. public boolean isAccountNonLocked() {
  63. return true;
  64. }
  65.  
  66. @Override
  67. public boolean isCredentialsNonExpired() {
  68. return true;
  69. }
  70.  
  71. @Override
  72. public boolean isEnabled() {
  73. return true;
  74. }
  75. }
  76.  
  77. package com.lixin.readinglist;
  78.  
  79. import org.springframework.data.jpa.repository.JpaRepository;
  80.  
  81. /**
  82. * @author lixin
  83. */
  84. public interface ReaderRepository extends JpaRepository<Reader, String> {
  85. }
  86.  
  87. package com.lixin.readinglist;
  88.  
  89. import org.springframework.beans.factory.annotation.Autowired;
  90. import org.springframework.context.annotation.Configuration;
  91. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  92. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  93. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  94. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  95. import org.springframework.security.core.userdetails.UserDetailsService;
  96.  
  97. /**
  98. * @author lixin
  99. */
  100. @Configuration
  101. @EnableWebSecurity
  102. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  103.  
  104. private final ReaderRepository readerRepository;
  105.  
  106. @Autowired
  107. public SecurityConfig(ReaderRepository readerRepository) {
  108. this.readerRepository = readerRepository;
  109. }
  110.  
  111. @Override
  112. protected void configure(HttpSecurity http) throws Exception {
  113. http
  114. .authorizeRequests()
  115. .antMatchers("/").access("hasRole('READER')")
  116. .antMatchers("/**").permitAll()
  117. .and()
  118. .formLogin()
  119. .loginPage("/login")
  120. .failureUrl("/login?error=true");
  121. }
  122.  
  123. @Override
  124. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  125. auth
  126. .userDetailsService((UserDetailsService) username -> readerRepository.findOne(username));
  127. }
  128. }
  129.  
  130. Error:(40, 86) java: method findOne in interface org.springframework.data.repository.query.QueryByExampleExecutor<T> cannot be applied to given types;
  131. required: org.springframework.data.domain.Example<S>
  132. found: java.lang.String
  133. reason: cannot infer type-variable(s) S
  134. (argument mismatch; java.lang.String cannot be converted to org.springframework.data.domain.Example<S>)
Add Comment
Please, Sign In to add comment