简体   繁体   English

如何从已登录的用户那里获取 email

[英]How to get the email from already logged user

I have this problem.我有这个问题。 Trying to get email of already register user who is logged in my application.试图获取登录我的应用程序的已注册用户的 email。 The idea is when i click "Profile", to see the email of logged user (and actually to see only the adds that the logged user have add in profile section).这个想法是当我单击“配置文件”时,查看登录用户的 email(实际上只看到登录用户在配置文件部分添加的添加)。 So my question is how can I connect html page with database and with current logged user?所以我的问题是如何将 html 页面与数据库和当前登录的用户连接起来? I have tried different ways, one of, but I get null for email, I have no idea why...我尝试了不同的方法,其中一种,但我得到 null 用于 email,我不知道为什么......

I use POSTGRESQL我使用 POSTGRESQL

This is my Entity这是我的实体

@Entity
@Table(name =  "username", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class Username {

@Id
@GeneratedValue(strategy =  GenerationType.IDENTITY)
private Long id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

private String email;

private String password;

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(
        name = "usernames_roles",
        joinColumns = @JoinColumn(
                name = "usernames_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(
                        name = "role_id", referencedColumnName = "id"))

private Collection<Role> roles;

public Username() {
    
}

This is security configuration这是安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfiguration  extends WebSecurityConfigurerAdapter{

@Autowired
private UserService userService;

@Bean
public BCryptPasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

@Bean
public DaoAuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
    auth.setUserDetailsService(userService);
    auth.setPasswordEncoder(passwordEncoder());
    return auth;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(authenticationProvider());
}


//.antMatchers("/", "/home**").anonymous()
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
    
    
    .antMatchers("/", "/home","/cars","/motors**").anonymous()
    .antMatchers("/", "/home","/cars","/motors**").permitAll()
    .antMatchers("/cars/add").authenticated()
    .and()
    .formLogin()
    .loginPage("/login")
        .permitAll()
        .defaultSuccessUrl("/")
        .and()
    .logout()
    .invalidateHttpSession(true)
    .clearAuthentication(true)
    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
    .logoutSuccessUrl("/login?logout")
    .permitAll();
}

First, you need setters and getters in your Username entity.首先,您需要在Username实体中设置 setter 和 getter。

Then, you get the current user's login by:然后,您通过以下方式获取当前用户的登录信息:

String login = "";
if (SecurityContextHolder.getContext().getAuthentication() != null)
login = SecurityContextHolder.getContext().getAuthentication().getName();

Then you get the user by login, depending on what you've implemented in terms of querying your database (eg. Repository, Service, etc).然后您通过登录获取用户,具体取决于您在查询数据库(例如存储库、服务等)方面实现的内容。

When you have your current userr, you just call its getEmail() .当您拥有当前用户时,您只需调用它的getEmail()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android如何在用户已经登录时获取用户Facebook信息 - Android how to get user Facebook information when user is already logged in 如何使用oauth2服务帐号获取登录用户的电子邮件? - How to get the email of the logged user with oauth2 service account? 如何从当前登录的用户获取数据 - How to get data from current logged user 如何从当前登录的用户 Firebase 获取数据 - How to get data from current logged in user Firebase 如何检测在我的GAE网站上已经从Google注销的用户 - How to detect user already logged out from Google at my GAE website 如何从spring security获取当前登录的用户对象? - How to get the current logged in user object from spring security? 如何获取当前登录用户的电子邮件地址? (Android Studio / Java / Firebase) - How do I get the email address of currently logged in user? (Android Studio / Java / Firebase) 用户已登录时从CAS登录页面重定向 - redirect from CAS login page when user is already logged in 在JAVA客户端中获取登录到用户电子邮件中的莲花笔记 - Get lotus notes logged in user's email in JAVA client 检查用户是否已经登录 - Check whether the user is already logged in or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM