【发布时间】:2020-07-08 15:57:24
【问题描述】:
在我的 Spring Boot 应用程序中,我有 2 种不同类型的用户 - 用户和供应商,它们存储在我的 SQL DB 的不同表中。
我只允许访问返回 JWT 的 /user/login 和 /vendor/login。
我无法理解如何配置 spring security 在有人请求 /user/login 时仅检查 USERS 表,而在供应商请求 /vendor/login 时仅检查 VENDORS 表。这可能吗?如果没有,任何人都可以建议我如何配置 Spring Security 以对来自不同表的用户进行身份验证?
这是我当前的配置,仅对用户进行身份验证 -
@Configuration
@EnableWebSecurity
public class SecurityConfigurer extends WebSecurityConfigurerAdapter {
@Autowired
private UserService myUserDetailsService; // this fetches data from the USERS table
@Autowired
private JwtRequestFilter jwtRequestFilter;
// *** How do I configure this to check both VENDORS OR USERS table? ***
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers("/user/auth/login").permitAll()
.antMatchers("/vendor/auth/login").permitAll()
.anyRequest().authenticated()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
我为用户和供应商实现了 UserDetailsService。这是 userService 的实现 -
@Service
public class UserService implements UserDetailsService {
@Autowired
private UserRepository repository;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
public UserService() {
}
public UserService(UserRepository repository) {
this.repository = repository;
}
public Users findOne(String id) {
Optional<Users> user = repository.findById(id);
return user.orElse(null);
}
public List<Users> findAll() {
List<Users> users = new ArrayList<>();
repository.findAll().forEach(users::add);
return users;
}
public Users insert(Users user) throws UnknownError {
// somecode here
}
public Users update(String id, Users user) {
// some code here
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
final Users user = findByEmail(username);
if (user == null) {
throw new UsernameNotFoundException("No user Found");
}
return new User(user.getEmail(), user.getPassword(), new ArrayList<>());
}
}
这里是UserController(VendorController和这个类似)-
@RestController
@RequestMapping(path = "/user")
public class AuthController {
@Autowired
UserService service;
@Autowired
AuthenticationManager authenticationManager;
@PostMapping(path = "/login")
public ResponseEntity<?> login(@RequestBody AuthenticationRequest form) throws Exception {
try {
authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(form.getEmail(), form.getPassword()));
} catch (Exception e) {
throw new Exception("Incorrect Credentials");
}
final UserDetails user = service.loadUserByUsername(form.getEmail());
Users returnedUser = service.insert(user);
ResponseStructure response = new ResponseStructure(true, returnedUser);
return ResponseEntity.ok(response);
}
【问题讨论】:
标签: java spring spring-boot authentication spring-security