[英]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.