add
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.example.server_site_api.config;
|
||||
|
||||
import com.example.server_site_api.servies.AuthenticationProviderCus;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.Customizer;
|
||||
@@ -13,6 +15,9 @@ import org.springframework.security.web.SecurityFilterChain;
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig {
|
||||
|
||||
@Autowired
|
||||
AuthenticationProviderCus authenticationProviderCus;
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
|
||||
@@ -29,6 +34,8 @@ public class SecurityConfig {
|
||||
)
|
||||
.httpBasic(Customizer.withDefaults());
|
||||
|
||||
http.authenticationProvider(authenticationProviderCus);
|
||||
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ public class Users {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
@@ -6,4 +6,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@ResponseBody
|
||||
public interface UserRepository extends JpaRepository<Users, Long> {
|
||||
|
||||
Users findByusername(String userName);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.example.server_site_api.servies;
|
||||
|
||||
import com.example.server_site_api.models.Users;
|
||||
import com.example.server_site_api.repositories.UserRepository;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.AuthenticationProvider;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AuthenticationProviderCus implements AuthenticationProvider {
|
||||
|
||||
@Autowired
|
||||
UserRepository ur;
|
||||
|
||||
@Override
|
||||
public @Nullable Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
||||
String username = authentication.getName();
|
||||
String password = authentication.getCredentials().toString();
|
||||
|
||||
Users user = ur.findByusername(username);
|
||||
|
||||
System.out.println("user" + user);
|
||||
|
||||
if (user.getPassword().equals(password)) {
|
||||
// true
|
||||
System.out.println("da vao");
|
||||
return new UsernamePasswordAuthenticationToken(
|
||||
username,
|
||||
password,
|
||||
AuthorityUtils.createAuthorityList("ROLE_ADMIN")
|
||||
);
|
||||
}else {
|
||||
throw new BadCredentialsException("tai khoan khong chinh xac");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> authentication) {
|
||||
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.example.server_site_api.servies;
|
||||
|
||||
import com.example.server_site_api.repositories.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CustomUserDetialService implements UserDetailsService {
|
||||
|
||||
@Autowired
|
||||
UserRepository ur;
|
||||
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
return User
|
||||
.withUsername("user")
|
||||
.password("{noop}123123")
|
||||
.roles("ADMIN")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user