lol
This commit is contained in:
@ -1,5 +1,2 @@
|
|||||||
eclipse.preferences.version=1
|
eclipse.preferences.version=1
|
||||||
encoding//src/main/java=UTF-8
|
|
||||||
encoding//src/main/resources=UTF-8
|
|
||||||
encoding//src/test/java=UTF-8
|
|
||||||
encoding/<project>=UTF-8
|
encoding/<project>=UTF-8
|
||||||
|
@ -21,11 +21,11 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class SecurityConfig {
|
public class SecurityConfig {
|
||||||
|
|
||||||
private final JwtAuthenticationFilter jwtAuthenticationFilter;
|
//private final JwtAuthenticationFilter jwtAuthenticationFilter;
|
||||||
private final UserDetailsService userDetailsService;
|
private final UserDetailsService userDetailsService;
|
||||||
|
|
||||||
public SecurityConfig(@Lazy JwtAuthenticationFilter jwtAuthenticationFilter, @Lazy UserDetailsService userDetailsService) {
|
public SecurityConfig(@Lazy JwtAuthenticationFilter jwtAuthenticationFilter, @Lazy UserDetailsService userDetailsService) {
|
||||||
this.jwtAuthenticationFilter = jwtAuthenticationFilter;
|
//this.jwtAuthenticationFilter = jwtAuthenticationFilter;
|
||||||
this.userDetailsService = userDetailsService;
|
this.userDetailsService = userDetailsService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,16 +47,28 @@ public class SecurityConfig {
|
|||||||
return authenticationConfiguration.getAuthenticationManager();
|
return authenticationConfiguration.getAuthenticationManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @Bean
|
||||||
|
// public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
|
// http.csrf(AbstractHttpConfigurer::disable)
|
||||||
|
// .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||||
|
// .authorizeHttpRequests(auth -> auth
|
||||||
|
// .requestMatchers("/api/user/login", "/api/user/register").permitAll()
|
||||||
|
// .anyRequest().authenticated()
|
||||||
|
// )
|
||||||
|
// .authenticationProvider(authenticationProvider())
|
||||||
|
// .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
||||||
|
//
|
||||||
|
// return http.build();
|
||||||
|
// }
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
//暂时开放所有权限
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
|
|
||||||
http.csrf(AbstractHttpConfigurer::disable)
|
http.csrf(AbstractHttpConfigurer::disable)
|
||||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
|
||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
.requestMatchers("/api/user/login", "/api/user/register").permitAll()
|
.anyRequest().permitAll()
|
||||||
.anyRequest().authenticated()
|
);
|
||||||
)
|
|
||||||
.authenticationProvider(authenticationProvider())
|
|
||||||
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
|
||||||
|
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
package co.jp.app.controller;
|
package co.jp.app.controller;
|
||||||
|
|
||||||
import co.jp.app.common.ApiResponse;
|
import java.util.HashMap;
|
||||||
import co.jp.app.dto.LoginDto;
|
import java.util.Map;
|
||||||
import co.jp.app.dto.RegistrationDto;
|
|
||||||
import co.jp.app.service.JwtService;
|
|
||||||
import jakarta.validation.Valid;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
@ -13,15 +11,18 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
|
|||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import co.jp.app.entity.ErrorEntity;
|
import co.jp.app.common.ApiResponse;
|
||||||
|
import co.jp.app.dto.LoginDto;
|
||||||
|
import co.jp.app.dto.RegistrationDto;
|
||||||
import co.jp.app.entity.UserEntity;
|
import co.jp.app.entity.UserEntity;
|
||||||
import co.jp.app.service.ErraService;
|
import co.jp.app.service.JwtService;
|
||||||
import co.jp.app.service.UserService;
|
import co.jp.app.service.UserService;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/user")
|
@RequestMapping("/api/user")
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
package co.jp.app.repository;
|
package co.jp.app.repository;
|
||||||
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import java.util.Optional;
|
||||||
import org.springframework.data.repository.query.Param;
|
|
||||||
|
|
||||||
import co.jp.app.entity.UserEntity;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.Optional;
|
import co.jp.app.entity.UserEntity;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface UserRepository extends JpaRepository<UserEntity, Integer> {
|
public interface UserRepository extends JpaRepository<UserEntity, Integer> {
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
package co.jp.app.repository;
|
|
||||||
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.data.repository.query.Param;
|
|
||||||
|
|
||||||
import co.jp.app.entity.UserEntity;
|
|
||||||
|
|
||||||
public interface userRepository extends JpaRepository<UserEntity, Integer> {
|
|
||||||
|
|
||||||
public default UserEntity getByName(@Param("name") String name) {
|
|
||||||
return getByName(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public default UserEntity getByEmail(@Param("email")String email) {
|
|
||||||
return getByEmail(email);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +1,8 @@
|
|||||||
package co.jp.app.service;
|
package co.jp.app.service;
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import co.jp.app.entity.UserEntity;
|
|
||||||
import co.jp.app.repository.userRepository;
|
|
||||||
import jakarta.transaction.Transactional;
|
|
||||||
|
|
||||||
=======
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import co.jp.app.dto.RegistrationDto;
|
|
||||||
import co.jp.app.entity.UserEntity;
|
|
||||||
import co.jp.app.repository.UserRepository;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
@ -26,39 +12,28 @@ import org.springframework.security.core.userdetails.UserDetailsService;
|
|||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import co.jp.app.entity.PetEntity;
|
|
||||||
import co.jp.app.repository.UploadRepository;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
>>>>>>> 34de018ef0606dec4f6de48c8cc5c3f073a1fdc6
|
|
||||||
|
import co.jp.app.dto.RegistrationDto;
|
||||||
|
import co.jp.app.entity.UserEntity;
|
||||||
|
import co.jp.app.repository.UserRepository;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService implements UserDetailsService {
|
public class UserService implements UserDetailsService {
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
private final userRepository userEntityRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public UserService(userRepository userEntityRepository, PasswordEncoder passwordEncoder )
|
|
||||||
{
|
|
||||||
this.userEntityRepository = userEntityRepository;
|
|
||||||
this.passwordEncoder= passwordEncoder;
|
|
||||||
|
|
||||||
=======
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.passwordEncoder = passwordEncoder;
|
this.passwordEncoder = passwordEncoder;
|
||||||
>>>>>>> 34de018ef0606dec4f6de48c8cc5c3f073a1fdc6
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public UserEntity registerNewUser(@NotNull RegistrationDto registrationDto) throws Exception {
|
public UserEntity registerNewUser(@NotNull RegistrationDto registrationDto) throws Exception {
|
||||||
|
|
||||||
if (userRepository.existsByEmail(registrationDto.getEmail())) {
|
if (userRepository.existsByEmail(registrationDto.getEmail())) {
|
||||||
throw new Exception("エラー:メール:" + registrationDto.getEmail() + " はすでに登録されました。");
|
throw new Exception("error: Email" + registrationDto.getEmail() + " had been used");
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity newUser = new UserEntity();
|
UserEntity newUser = new UserEntity();
|
||||||
@ -75,7 +50,8 @@ public class UserService implements UserDetailsService {
|
|||||||
UserEntity userEntity = userRepository.findByEmail(email)
|
UserEntity userEntity = userRepository.findByEmail(email)
|
||||||
.orElseThrow(() -> new UsernameNotFoundException(email + " not found"));
|
.orElseThrow(() -> new UsernameNotFoundException(email + " not found"));
|
||||||
|
|
||||||
Collection<? extends GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")); // 示例:给所有用户一个ROLE_USER权限
|
Collection<? extends GrantedAuthority> authorities = Collections
|
||||||
|
.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
|
||||||
|
|
||||||
return new User(
|
return new User(
|
||||||
userEntity.getEmail(),
|
userEntity.getEmail(),
|
||||||
@ -84,7 +60,7 @@ public class UserService implements UserDetailsService {
|
|||||||
true, // accountNonExpired
|
true, // accountNonExpired
|
||||||
true, // credentialsNonExpired
|
true, // credentialsNonExpired
|
||||||
true, // accountNonLocked
|
true, // accountNonLocked
|
||||||
authorities // 用户的权限集合
|
authorities // role
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
2
target/classes/.gitignore
vendored
Normal file
2
target/classes/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/META-INF/
|
||||||
|
/co/
|
@ -1,6 +1,6 @@
|
|||||||
#Generated by Maven Integration for Eclipse
|
#Generated by Maven Integration for Eclipse
|
||||||
#Mon May 12 14:19:16 JST 2025
|
#Mon May 12 15:57:02 JST 2025
|
||||||
m2e.projectLocation=C\:\\Users\\ichbi\\OneDrive\\\u30C7\u30B9\u30AF\u30C8\u30C3\u30D7\\dog-1
|
m2e.projectLocation=C\:\\Users\\Administrator\\git\\Dog-1
|
||||||
m2e.projectName=dog-1
|
m2e.projectName=dog-1
|
||||||
groupId=co.jp.app
|
groupId=co.jp.app
|
||||||
artifactId=dog-2
|
artifactId=dog-2
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user