Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lixin.readinglist;
- import org.springframework.data.annotation.Id;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.SimpleGrantedAuthority;
- import org.springframework.security.core.userdetails.UserDetails;
- import javax.persistence.Entity;
- import java.util.Collection;
- import java.util.Collections;
- /**
- * @author lixin
- */
- @Entity
- public class Reader implements UserDetails {
- private static final long serialVersionUID = 1L;
- @Id
- private String username;
- private String fullname;
- private String password;
- @Override
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getFullname() {
- return fullname;
- }
- public void setFullname(String fullname) {
- this.fullname = fullname;
- }
- @Override
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public Collection<? extends GrantedAuthority> getAuthorities() {
- return Collections.singletonList(new SimpleGrantedAuthority("READER"));
- }
- @Override
- public boolean isAccountNonExpired() {
- return true;
- }
- @Override
- public boolean isAccountNonLocked() {
- return true;
- }
- @Override
- public boolean isCredentialsNonExpired() {
- return true;
- }
- @Override
- public boolean isEnabled() {
- return true;
- }
- }
- package com.lixin.readinglist;
- import org.springframework.data.jpa.repository.JpaRepository;
- /**
- * @author lixin
- */
- public interface ReaderRepository extends JpaRepository<Reader, String> {
- }
- package com.lixin.readinglist;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- import org.springframework.security.config.annotation.web.builders.HttpSecurity;
- import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- import org.springframework.security.core.userdetails.UserDetailsService;
- /**
- * @author lixin
- */
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- private final ReaderRepository readerRepository;
- @Autowired
- public SecurityConfig(ReaderRepository readerRepository) {
- this.readerRepository = readerRepository;
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http
- .authorizeRequests()
- .antMatchers("/").access("hasRole('READER')")
- .antMatchers("/**").permitAll()
- .and()
- .formLogin()
- .loginPage("/login")
- .failureUrl("/login?error=true");
- }
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth
- .userDetailsService((UserDetailsService) username -> readerRepository.findOne(username));
- }
- }
- Error:(40, 86) java: method findOne in interface org.springframework.data.repository.query.QueryByExampleExecutor<T> cannot be applied to given types;
- required: org.springframework.data.domain.Example<S>
- found: java.lang.String
- reason: cannot infer type-variable(s) S
- (argument mismatch; java.lang.String cannot be converted to org.springframework.data.domain.Example<S>)
Add Comment
Please, Sign In to add comment