me工具

ebdffaop  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(226)

RememberConfigurer通过调用其方法loaduserbyusername()来使用userdetailsservice,问题是,我希望它传递用户的电子邮件,而不是用户名。它只适用于简单的登录,但不适用于“记住我”选项。如何使“记住我”功能能够将电子邮件(而不是用户名)传递给userdetailsservice.loaduserbyusername()?登录表单:

<form action="login" method="POST" >

              <legend>Sing In</legend>

              <div class="form-group">

                <div class="input-group">
                  <label class="form-label" for="email">
                    Email
                  </label>

                  <div class="form-input-field">

                      <input id="email" name="username"
                             type="text" autocomplete="off"/>
                  </div>
                </div>

              </div>

              <div class="form-group">

                <div class="input-group">
                  <label class="form-label" for="password">
                    Password
                  </label>

                  <div class="form-input-field">

                      <input id="password" name="password"
                             type="password" autocomplete="off" />
                  </div>
                </div>

              </div>

              <div class="form-group">

              <div class="input-group">
                <label id="checkbox-label" class="form-label" for="remember">
                  Remember me
                </label>

                <div class="form-input-field">

                    <input id="remember" name="remember"
                           type="checkbox" />
                </div>

              </div>

              <div id="submit-button">

                  <input type="submit" value="Sign In" />

              </div>

      </form>

Web安全配置:

@Override
    public void configure(HttpSecurity httpSecurity) throws Exception {

        httpSecurity.formLogin()
                        .loginPage("/login")
                        .permitAll()
                        .defaultSuccessUrl("/account")
                    .and()
                        .rememberMe()
                        .key("rem-me-key")
                        .userDetailsService(userDetailsService)
                        .rememberMeParameter("remember")
                        .rememberMeCookieName("rememberMeCookie")
                        .tokenValiditySeconds(60 * 60 * 24 * 3)
                    .and()
                        .logout()
                        .deleteCookies("JSESSIONID")
                        .permitAll();

        httpSecurity.authorizeRequests()
                    .mvcMatchers("/admin").hasRole("ADMIN")
                    .mvcMatchers("/account").hasRole("CUSTOMER")
                    .mvcMatchers("/cart").hasRole("CUSTOMER")
                    .anyRequest().permitAll();

        httpSecurity.csrf()
                    .disable();

    }

用户详细信息服务实现:

@Service
public class AppUserDetailsService implements UserDetailsService {

    private static final Logger log =
            LogManager.getLogger(AppUserDetailsService.class.getName());

    private AppUserRetrievalRepository appUserRetrievalRepository;

    public AppUserDetailsService(
            @Autowired
            AppUserRetrievalRepository appUserRetrievalRepository) {

        this.appUserRetrievalRepository = appUserRetrievalRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String email)
            throws UsernameNotFoundException {

        log.info("Trying to load user by name: " + email);

        Optional<AppUser> optUser =
                appUserRetrievalRepository.getUserByEmail(email);

        UserDetails userDetails =
                new AppUserDetails(optUser.orElseThrow(
                        () -> new UsernameNotFoundException(email + " cannot be found") )
                );

        return userDetails;
    }
}

显示问题的日志:~当我在未选中“记住我”选项的情况下使用登录时:


***LOG4J***Trying to load user by name: myEmailAddress@example.com

~当我使用选中“记住我”选项的登录时:


***LOG4J***Trying to load user by name: cody

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题