我的数据库中有一个表,其中包含以下信息:
Nom=name。Prénom=firstname。
我正在尝试,一旦用户登录,检索整个用户的字段,除了:密码。
例如,
如果我在日志页面上键入用户名和密码,我想检索:
还有,
我已经知道如何简单地检索用户名,但是我一直无法获取整个用户字段。。
这是我的文件,
用户实体
@Entity
@Table(name = "users")
@NoArgsConstructor
@Getter @Setter
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "nom", length = 50)
private String name;
@Column(name = "prenom", length = 50)
private String firstname;
@Column(name = "username", length = 50, unique = true)
private String username;
@Column
private String email;
@Column
private String role;
@Column()
private String password;
}
AuthUser详情|自定义用户详情
public class AuthUserDetails implements UserDetails {
private final String name;
private final String firstname;
private final String username;
private final String email;
private final String password;
private final List<GrantedAuthority> authorities;
public AuthUserDetails(User user) {
this.name = user.getUsername();
this.firstname = user.getFirstname();
this.username = user.getUsername();
this.email = user.getEmail();
this.password = user.getPassword();
this.authorities = Arrays.stream(user.getRole().split(","))
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return authorities;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
AuthUserDetailsService |自定义UserDetailsService
// Handle UserAuthentication from Database | Personal User's credentials
@Service
public class AuthUserDetailsService implements UserDetailsService {
private final UserRepository userRepository;
@Autowired
public AuthUserDetailsService(UserRepository userRepository) {
this.userRepository = userRepository;
}
// ************************ By | Username ************************ //
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// Find the username in the DB
Optional<User> user = userRepository.findByUsername(username);
user.orElseThrow(() -> new UsernameNotFoundException("Not found " + username));
return user.map(AuthUserDetails::new).get();
}
}
用户服务
@Service
public class UserService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final AuthenticationManager authenticationManager;
private final JwtProvider jwtProvider;
@Autowired
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder,
AuthenticationManager authenticationManager, JwtProvider jwtProvider) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
this.authenticationManager = authenticationManager;
this.jwtProvider = jwtProvider;
}
// ****************************** Login ***************************** //
public AuthenticationResponse login(AuthMapping authMapping) {
Authentication authenticate = authenticationManager.authenticate(new
UsernamePasswordAuthenticationToken(authMapping.getUsername(), authMapping.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authenticate);
String authenticationToken = jwtProvider.generateToken(authenticate);
return new AuthenticationResponse(authenticationToken, authMapping.getUsername());
}
}
AuthMapping |这是用户要登录的类型
@Getter @Setter
public class AuthMapping {
private String username;
private String password;
}
AuthentiationACK|这是我们成功登录后检索到的内容
@Data
@AllArgsConstructor
public class AuthenticationResponse {
private String authenticationToken;
private String username;
}
html" target="_blank">安全配置
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// *************************************************************************** //
private final UserDetailsService userDetailsService;
private final PasswordEncoder passwordEncoder;
private final JwtAuthenticationFilter jwtAuthenticationFilter;
@Autowired
public SecurityConfig(@Qualifier("authUserDetailsService") UserDetailsService userDetailsService, PasswordEncoder passwordEncoder, JwtAuthenticationFilter jwtAuthenticationFilter) {
this.userDetailsService = userDetailsService;
this.passwordEncoder = passwordEncoder;
this.jwtAuthenticationFilter = jwtAuthenticationFilter;
}
// *************************************************************************** //
// Authorization
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/api/users/register").permitAll()
.antMatchers("/api/users/login").permitAll()
.anyRequest().authenticated();
// JWT Authentication
http.addFilterBefore(this.jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
// CORS
http.addFilterBefore(new CorsFilter(), ChannelProcessingFilter.class);
}
// *************************************************************************** //
/**
* Handle The Authentication in Spring | InMemory/LDAP/JDBC
* The handle is made through a service : UserDetailsService
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(this.passwordEncoder);
/*
* Read user from the database
* Spring needs to decode the password before performing the authentication
*/
}
// *************************************************************************** //
@Bean(BeanIds.AUTHENTICATION_MANAGER)
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
UserController |登录
// User | Login
@PostMapping("/login")
public AuthenticationResponse login(@RequestBody AuthMapping authMapping) {
return userService.login(authMapping);
}
请问怎么解决?
提前谢谢你。
您可以编写另一个自定义方法来检索结果:我提供了用Kotlin编写的一段代码。
fun currentUser(): UserDto {
val auth = SecurityContextHolder.getContext().authentication
//auth.principal this is username
val optional = repository.findByEmail(auth.principal.toString())
if (!optional.isPresent) throw ErrorWithUserTokenException(auth.principal.toString())
return UserDto.toDto(optional.get())
}
data class UserDto(
var id: Long,
var username: String
) {
companion object {
fun toDto(user: User): UserDto {
return user.run { UserDto(id!!, username) }
}
}
}
我认为,至少,这个答案为您提供了一个解决问题的想法:)
我用的是Spring Security。配置的一部分: 例如,我如何在Spring MVC控制器中获得当前用户平衡?
我有一个辅助项目网站,人们可以在那里评论东西,现在我需要显示用户的评论和评论者的用户名。问题是,我无法获得登录用户的用户名。 评论示例: 1) 评论人-Shelo 测试评论 2) 评论人-ProGa 测试评论2 现在的问题是,如果我试图获取“当前登录”用户的用户名,当用户登录时,两条评论都会有相同的用户名 上面的代码示例是您当前登录用户的方式。因此,如果我使用这段代码,并尝试通过
问题内容: 我已经搜索了许多网站,但没有遇到任何从单个数据库获取表名的细节的代码或教程。 假设我有4个数据库,并且我想要数据库中所有表的名称,我可以使用什么查询? 问题答案:
我需要一些与Firebase实时数据库请求的帮助。 我想实现一些类似搜索屏幕,将显示所有用户,但我有一个问题,从Firebase检索数据。 下面是向Firebase发送请求的方法。 和检索用户计数。 有人能解释我该怎么办?
喂! 我一直在做一个web项目,用户可以注册和登录。我也想添加一个配置文件,他们提供的一些信息可以在“配置文件”页面上显示。 我已经使用firebase auth(与电子邮件和密码)和rt数据库存储额外的信息,如‘姓名','生物’等。 我已经尝试过这里其他问题的解决方案,但似乎没有一个对我有效。 下面是我的数据库的外观: 下面是配置文件页面的一些代码: 当我测试代码时,我在控制台上得到了这个错误: