[英]Android how to get user Facebook information when user is already logged in
[英]How to get the email from already logged user
我有這個問題。 試圖獲取登錄我的應用程序的已注冊用戶的 email。 這個想法是當我單擊“配置文件”時,查看登錄用戶的 email(實際上只看到登錄用戶在配置文件部分添加的添加)。 所以我的問題是如何將 html 頁面與數據庫和當前登錄的用戶連接起來? 我嘗試了不同的方法,其中一種,但我得到 null 用於 email,我不知道為什么......
我使用 POSTGRESQL
這是我的實體
@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() {
}
這是安全配置
@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();
}
首先,您需要在Username
實體中設置 setter 和 getter。
然后,您通過以下方式獲取當前用戶的登錄信息:
String login = "";
if (SecurityContextHolder.getContext().getAuthentication() != null)
login = SecurityContextHolder.getContext().getAuthentication().getName();
然后您通過登錄獲取用戶,具體取決於您在查詢數據庫(例如存儲庫、服務等)方面實現的內容。
當您擁有當前用戶時,您只需調用它的getEmail()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.