Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This Service class for providing the user credentials from the database.
- *
- */
- @Service
- public class CustomUserDetailsService implements UserDetailsService {
- @Autowired
- CustomUserRepository customUserRepository;
- @Override
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- CustomUser cusUser = customUserRepository.findOneByUsername(username);
- return cusUser;
- }
- }
- import org.springframework.data.jpa.repository.JpaRepository;
- import project.domain.CustomUser;
- public interface CustomUserRepository extends JpaRepository<AppUser, Long> {
- CustomUser findOneByUsername(String username);
- }
- import java.util.ArrayList;
- import java.util.Collection;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.SimpleGrantedAuthority;
- import org.springframework.security.core.userdetails.UserDetails;
- import com.fasterxml.jackson.annotation.JsonIgnore;
- @Entity
- public class CustomUser implements UserDetails {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- private String name;
- @Column(unique = true)
- private String username;
- private String password;
- private String role;
- private String location;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getRole() {
- return role;
- }
- public void setRole(String role) {
- this.role = role;
- }
- public String getLocation() {
- return location;
- }
- public void setLocation(String location) {
- this.location = location;
- }
- @JsonIgnore
- @Override
- public boolean isEnabled() {
- return true;
- }
- @JsonIgnore
- @Override
- public boolean isCredentialsNonExpired() {
- return true;
- }
- @JsonIgnore
- @Override
- public boolean isAccountNonLocked() {
- return true;
- }
- @JsonIgnore
- @Override
- public boolean isAccountNonExpired() {
- return true;
- }
- @JsonIgnore
- @Override
- public Collection<? extends GrantedAuthority> getAuthorities() {
- Collection<GrantedAuthority> authorities = new ArrayList<>();
- authorities.add(new SimpleGrantedAuthority(role));
- return authorities;
- }
- }
- import java.util.List;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.data.jpa.repository.Query;
- import org.springframework.data.repository.query.Param;
- import compliance.domain.Results;
- public interface ResultsRepository extends JpaRepository<Alerts, Long> {
- @Query(value ="SELECT RESULTS.RESULT, RESULTS.LEAGUEID,RESULTS.HOMETEAM, RESULTS.AWAYTEAM,LOCATION.LOCATION FROM RESULTS n" +
- "INNER JOIN n" +
- "LOCATION ON RESULTS.LEAGUEID=LOCATION.LEAGUEIDn" +
- "WHERE LOCATION.LOCATION= :location", nativeQuery = true)
- List<Results> findByLocation(@Param("location") String location);
- }
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import compliance.domain.Results;
- import compliance.repository.*;
- import compliance.services.CustomUserDetailsService;
- @RestController
- @RequestMapping(value = "/api")
- public class ResultsController {
- @Autowired
- private ResultsRepository ResultssRepository;
- private CustomUserDetailsService userDet;
- private String location = "England";
- @RequestMapping(value = "/alerts", method = RequestMethod.GET)
- public List<Alerts> Alerts() {
- // THIS DOESN'T WORK
- // user = (CustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- // System.out.println("UserRestController: " + user.getLocation());
- // System.out.println("user.getLocation());
- /** this returns every result not what I want **/
- //return ResultsRepository.findAll(new Sort("leagueid"));
- /** I think I need to be doing something like this to try get the location of logged in user but it doesn't work **/
- ResultsRepository.findByLocation(user.getLocation().toString());
- /** This works it filters the table based on the hard coded location from above - obvs not really what im looking for but at least I know my SQL query does what it should **/
- return AlertsRepository.findByLocation(location);
- }
- }
Add Comment
Please, Sign In to add comment