auth added
This commit is contained in:
		
							parent
							
								
									dcfa2f0cac
								
							
						
					
					
						commit
						9dbdc448fc
					
				@ -1,11 +1,13 @@
 | 
				
			|||||||
package org.example.config;
 | 
					package org.example.config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import lombok.RequiredArgsConstructor;
 | 
					import lombok.RequiredArgsConstructor;
 | 
				
			||||||
 | 
					import org.example.service.UserService;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
import org.springframework.context.annotation.Configuration;
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
 | 
					import org.springframework.security.authentication.AuthenticationManager;
 | 
				
			||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
 | 
					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.builders.HttpSecurity;
 | 
				
			||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 | 
					import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 | 
				
			||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
 | 
					 | 
				
			||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 | 
					import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 | 
				
			||||||
import org.springframework.security.core.userdetails.UserDetailsService;
 | 
					import org.springframework.security.core.userdetails.UserDetailsService;
 | 
				
			||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 | 
					import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 | 
				
			||||||
@ -23,11 +25,14 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 | 
				
			|||||||
        http
 | 
					        http
 | 
				
			||||||
                .csrf().disable()
 | 
					                .csrf().disable()
 | 
				
			||||||
                .authorizeRequests()
 | 
					                .authorizeRequests()
 | 
				
			||||||
                .antMatchers("/api/**").permitAll()
 | 
					                .antMatchers("/h2-console/**").permitAll()
 | 
				
			||||||
//                .antMatchers("api/{username}/passing").hasAuthority("ROLE_ADMIN")
 | 
					                .antMatchers("api/{username}/passing").hasAuthority("ROLE_ADMIN")
 | 
				
			||||||
//                .antMatchers("api/{username}/**").hasAnyAuthority("ROLE_USER", "ROLE_ADMIN")
 | 
					                .antMatchers("api/{username}/**").hasAnyAuthority("ROLE_USER", "ROLE_ADMIN")
 | 
				
			||||||
                .anyRequest().authenticated()
 | 
					                .anyRequest().authenticated()
 | 
				
			||||||
                .and().httpBasic().and().headers().frameOptions().disable();
 | 
					                .and()
 | 
				
			||||||
 | 
					                .httpBasic()
 | 
				
			||||||
 | 
					                .and()
 | 
				
			||||||
 | 
					                .headers().frameOptions().disable();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
@ -35,7 +40,16 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 | 
				
			|||||||
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
 | 
					        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private PasswordEncoder passwordEncoder() {
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public PasswordEncoder passwordEncoder() {
 | 
				
			||||||
        return new BCryptPasswordEncoder();
 | 
					        return new BCryptPasswordEncoder();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public AuthenticationManager authenticationManager(HttpSecurity http, UserService personDetailsService) throws Exception {
 | 
				
			||||||
 | 
					        return http.getSharedObject(AuthenticationManagerBuilder.class)
 | 
				
			||||||
 | 
					                .userDetailsService(userDetailsService)
 | 
				
			||||||
 | 
					                .and()
 | 
				
			||||||
 | 
					                .build();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -11,6 +11,8 @@ import org.springframework.data.domain.Pageable;
 | 
				
			|||||||
import org.springframework.http.ResponseEntity;
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
import org.springframework.web.bind.annotation.*;
 | 
					import org.springframework.web.bind.annotation.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@RequestMapping("api/{username}")
 | 
					@RequestMapping("api/{username}")
 | 
				
			||||||
@RequiredArgsConstructor
 | 
					@RequiredArgsConstructor
 | 
				
			||||||
@ -37,8 +39,8 @@ public class UserController {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @GetMapping("/passing")
 | 
					    @GetMapping("/passing")
 | 
				
			||||||
    public ResponseEntity<PassingDTO> getPassingByUsername(@PathVariable String username) {
 | 
					    public List<PassingDTO> getPassingByUsername(@PathVariable String username) {
 | 
				
			||||||
        return ResponseEntity.ok(passingService.getPassingByUsername(username));
 | 
					        return passingService.getPassingByUsername(username);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @GetMapping("/passing/paginated")
 | 
					    @GetMapping("/passing/paginated")
 | 
				
			||||||
@ -48,4 +50,9 @@ public class UserController {
 | 
				
			|||||||
        Pageable pageable = PageRequest.of(page, size);
 | 
					        Pageable pageable = PageRequest.of(page, size);
 | 
				
			||||||
        return ResponseEntity.ok(passingService.getAllPassingPaginated(pageable));
 | 
					        return ResponseEntity.ok(passingService.getAllPassingPaginated(pageable));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//    @PostMapping("/passing/new")
 | 
				
			||||||
 | 
					//    public ResponseEntity<PassingDTO> createPassing(@RequestBody PassingDTO dto) {
 | 
				
			||||||
 | 
					//        return ResponseEntity.ok
 | 
				
			||||||
 | 
					//    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -6,7 +6,7 @@ import org.hibernate.mapping.Set;
 | 
				
			|||||||
@Data
 | 
					@Data
 | 
				
			||||||
public class PassingDTO {
 | 
					public class PassingDTO {
 | 
				
			||||||
    private Long id;
 | 
					    private Long id;
 | 
				
			||||||
    private String Username;
 | 
					    private String username;
 | 
				
			||||||
    private String type;
 | 
					    private String type;
 | 
				
			||||||
    private String time;
 | 
					    private String time;
 | 
				
			||||||
    private Long code;
 | 
					    private Long code;
 | 
				
			||||||
 | 
				
			|||||||
@ -41,27 +41,23 @@ public class User implements UserDetails{
 | 
				
			|||||||
        return List.of();
 | 
					        return List.of();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public boolean isAccountNonExpired() {
 | 
					    public boolean isAccountNonExpired() {
 | 
				
			||||||
        return false;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public boolean isAccountNonLocked() {
 | 
					    public boolean isAccountNonLocked() {
 | 
				
			||||||
        return false;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public boolean isCredentialsNonExpired() {
 | 
					    public boolean isCredentialsNonExpired() {
 | 
				
			||||||
        return false;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public boolean isEnabled() {
 | 
					    public boolean isEnabled() {
 | 
				
			||||||
        return false;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -5,10 +5,11 @@ import org.springframework.data.domain.Page;
 | 
				
			|||||||
import org.springframework.data.domain.Pageable;
 | 
					import org.springframework.data.domain.Pageable;
 | 
				
			||||||
import org.springframework.data.jpa.repository.JpaRepository;
 | 
					import org.springframework.data.jpa.repository.JpaRepository;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Optional;
 | 
					import java.util.Optional;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public interface PassingRepository extends JpaRepository<Passing, Long> {
 | 
					public interface PassingRepository extends JpaRepository<Passing, Long> {
 | 
				
			||||||
    Optional<Passing> findByUsername(String login);
 | 
					    List<Passing> findByUsername(String login);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    Page<Passing> findAll(Pageable pageable);
 | 
					    Page<Passing> findAll(Pageable pageable);
 | 
				
			||||||
 | 
				
			|||||||
@ -4,8 +4,12 @@ import org.example.dto.PassingDTO;
 | 
				
			|||||||
import org.springframework.data.domain.Page;
 | 
					import org.springframework.data.domain.Page;
 | 
				
			||||||
import org.springframework.data.domain.Pageable;
 | 
					import org.springframework.data.domain.Pageable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public interface PassingService {
 | 
					public interface PassingService {
 | 
				
			||||||
    PassingDTO getPassingByUsername(String username);
 | 
					    List<PassingDTO> getPassingByUsername(String username);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Page<PassingDTO> getAllPassingPaginated(Pageable pageable);
 | 
					    Page<PassingDTO> getAllPassingPaginated(Pageable pageable);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    PassingDTO createPassing(PassingDTO dto);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -8,4 +8,6 @@ public interface UserService {
 | 
				
			|||||||
    UserDTO getUserInfoByUsername(String username);
 | 
					    UserDTO getUserInfoByUsername(String username);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    UserDTO patchUserByUsername(String username);
 | 
					    UserDTO patchUserByUsername(String username);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@ package org.example.service.impl;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import lombok.RequiredArgsConstructor;
 | 
					import lombok.RequiredArgsConstructor;
 | 
				
			||||||
import org.example.dto.PassingDTO;
 | 
					import org.example.dto.PassingDTO;
 | 
				
			||||||
 | 
					import org.example.entity.Passing;
 | 
				
			||||||
import org.example.exception.UserNotFoundException;
 | 
					import org.example.exception.UserNotFoundException;
 | 
				
			||||||
import org.example.repository.PassingRepository;
 | 
					import org.example.repository.PassingRepository;
 | 
				
			||||||
import org.example.service.PassingService;
 | 
					import org.example.service.PassingService;
 | 
				
			||||||
@ -10,13 +11,16 @@ import org.springframework.data.domain.Page;
 | 
				
			|||||||
import org.springframework.data.domain.Pageable;
 | 
					import org.springframework.data.domain.Pageable;
 | 
				
			||||||
import org.springframework.stereotype.Service;
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					import java.util.stream.Collectors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Service
 | 
					@Service
 | 
				
			||||||
@RequiredArgsConstructor
 | 
					@RequiredArgsConstructor
 | 
				
			||||||
public class PassingServiceImpl implements PassingService {
 | 
					public class PassingServiceImpl implements PassingService {
 | 
				
			||||||
    private final PassingRepository passingRepository;
 | 
					    private final PassingRepository passingRepository;
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public PassingDTO getPassingByUsername(String Username) {
 | 
					    public List<PassingDTO> getPassingByUsername(String Username) {
 | 
				
			||||||
        return passingRepository.findByUsername(Username).map(PassingMapper::convertToDto).orElseThrow(() -> new UserNotFoundException("Посещения не найдены"));
 | 
					        return passingRepository.findByUsername(Username).stream().map(PassingMapper::convertToDto).collect(Collectors.toList());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -24,4 +28,16 @@ public class PassingServiceImpl implements PassingService {
 | 
				
			|||||||
    public Page<PassingDTO> getAllPassingPaginated(Pageable pageable) {
 | 
					    public Page<PassingDTO> getAllPassingPaginated(Pageable pageable) {
 | 
				
			||||||
        return passingRepository.findAll(pageable).map(PassingMapper::convertToDto);
 | 
					        return passingRepository.findAll(pageable).map(PassingMapper::convertToDto);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public PassingDTO createPassing(PassingDTO dto) {
 | 
				
			||||||
 | 
					        Passing passing = new Passing();
 | 
				
			||||||
 | 
					        passing.setId(dto.getId());
 | 
				
			||||||
 | 
					        passing.setUsername(dto.getUsername());
 | 
				
			||||||
 | 
					        passing.setType(dto.getType());
 | 
				
			||||||
 | 
					        passing.setTime(dto.getType());
 | 
				
			||||||
 | 
					        passing.setCode(dto.getCode());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return PassingMapper.convertToDto(passingRepository.save(passing));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -2,3 +2,4 @@ username;type;time;code
 | 
				
			|||||||
pivanov;Карта;12:00;1234567890123456789
 | 
					pivanov;Карта;12:00;1234567890123456789
 | 
				
			||||||
ipetrov;Вход со смартфона;13:00;9223372036854775807
 | 
					ipetrov;Вход со смартфона;13:00;9223372036854775807
 | 
				
			||||||
asemenov;Карта;10:00;1234567890123456789
 | 
					asemenov;Карта;10:00;1234567890123456789
 | 
				
			||||||
 | 
					pivanov;Вход со смартфона;15:00;1234567890123456789
 | 
				
			||||||
 | 
				
			|||||||
		
		
			
  | 
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user