RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 649572
Accepted
Nick
Nick
Asked:2020-04-07 16:01:00 +0000 UTC2020-04-07 16:01:00 +0000 UTC 2020-04-07 16:01:00 +0000 UTC

如何在 SpringSecurity 中将用户数据写入日志文件

  • 772

大家下午好。对他进入SpringSecurity所连接的MVC应用程序时如何将用户数据写入日志文件的问题感兴趣。对于注册页面,我将用户数据写入如下:在页面控制器中,我创建了一个记录器,并在 saveUser 方法中将用户信息写入日志文件,如下所示。

@Controller
@RequestMapping("/registration")
public class RegistrationController {

    private static final Logger logger = Logger.getLogger(RegistrationController.class.getName());

    @Autowired
    PersonService personService;

    @Autowired
    CityService cityService;

    @Autowired
    MessageSource messageSource;

    @Autowired
    RoleService roleService;

    @RequestMapping(method = RequestMethod.GET)
    public String renderRegistration(ModelMap model) {
        Persons person = new Persons();

        model.addAttribute("userForm", person);
        model.addAttribute("edit", false);
        model.addAttribute("loggedinuser", getPrincipal());
        return "registration";
    }

    @RequestMapping(value = "/newUser", method = RequestMethod.POST)
    public String saveUser(@Valid @ModelAttribute("userForm") Persons person, BindingResult result,
                           ModelMap model) {

        List<FieldError> errors = new ArrayList<>();

        if (result.hasErrors()) {
            return "errorPage";
        }

        if (person.getNickname().isEmpty()) {
            FieldError nicknameError = new FieldError("person", "nickname", messageSource.getMessage("NotEmpty.person.nickname", new String[]{person.getNickname()}, Locale.getDefault()));
            errors.add(nicknameError);
        }

        if (!personService.isPersonsNicknameUnique(person.getPersonId(), person.getNickname())) {
            FieldError nicknameUniqError = new FieldError("person", "nickname", messageSource.getMessage("non.unique.nickname", new String[]{person.getNickname()}, Locale.getDefault()));
            errors.add(nicknameUniqError);
        }


        if (person.getPassword().isEmpty()) {
            FieldError passwordError = new FieldError("person", "password", messageSource.getMessage("NotEmpty.person.password", new String[]{person.getNickname()}, Locale.getDefault()));
            errors.add(passwordError);
        }

        if (person.getFirstName().isEmpty()) {
            FieldError firstNameError = new FieldError("person", "firstName", messageSource.getMessage("NotEmpty.person.firstName", new String[]{person.getNickname()}, Locale.getDefault()));
            errors.add(firstNameError);
        }

        if (person.getLastName().isEmpty()) {
            FieldError lastNameError = new FieldError("person", "lastName", messageSource.getMessage("NotEmpty.person.lastName", new String[]{person.getNickname()}, Locale.getDefault()));
            errors.add(lastNameError);
        }

        if (person.getEmail().isEmpty()) {
            FieldError emailError = new FieldError("person", "email", messageSource.getMessage("NotEmpty.person.email", new String[]{person.getNickname()}, Locale.getDefault()));
            errors.add(emailError);
        }

        if (person.getCity().equals(null)) {
            FieldError cityError = new FieldError("person", "city", messageSource.getMessage("NotEmpty.person.city", new String[]{person.getNickname()}, Locale.getDefault()));
            errors.add(cityError);
        }
        if (!errors.isEmpty()) {

            for (FieldError error : errors) {
                result.addError(error);
            }
            return "registration";
        }
//        person.setRole(roleService.findByType("USER"));
        personService.savePerson(person);

        if (Const.DEBUG) {
            if (logger.isDebugEnabled()) {
                logger.debug("person: id-" + person.getPersonId() +
                        " Nickname-" + person.getNickname() +
                        " Password-" + person.getPassword() +
                        " Lastname-" + person.getLastName() +
                        " FirstName-" + person.getFirstName() +
                        " Email-" + person.getEmail() +
                        " City-" + person.getCity().getCityName() +
                        " MobileNumber-" + person.getMobileNumber());
            }
        }

        return "success";
    }

    @ModelAttribute("rollers")
    public List<Rollers> getRollers() { return roleService.findAll();}

    @ModelAttribute("cities")
    public List<Cities> initializeCities() {
        return cityService.getAll();
    }

    private String getPrincipal() {
        String userName = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetails) {
            userName = ((UserDetails) principal).getUsername();
        } else {
            userName = principal.toString();
        }
        return userName;
    }
}

我需要在用户登录时做类似的操作,但是由于我连接了SpringSecurity,它处理了POST方法,最后我不明白如何将用户数据写入文件。PS:代码不要骂我,我知道不是很好)

试图这样做:

   @Component
public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    public static final Logger logger = Logger.getLogger(CustomSuccessHandler.class.getName());

    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    @Override
    protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException {
        String targetUrl = determineTargetUrl(authentication);

        if (response.isCommitted()) {
            System.out.println("Can't redirect");
            return;
        }

//        String userName;
//        Object principal = authentication.getPrincipal();
//        if (principal instanceof UserDetails) {
//            userName = ((UserDetails) principal).getUsername();
//        } else {
//            userName = principal.toString();
//        }
//        if (Const.DEBUG) {
//            if (logger.isDebugEnabled()) {
                logger.debug("person: Nickname-" + authentication.getPrincipal().toString());
                logger.debug("person: Nickname-");
//            }
//        }

        redirectStrategy.sendRedirect(request, response, targetUrl);
    }

    /*
     * This method extracts the roles of currently logged-in user and returns
     * appropriate URL according to his/her role.
     */
    protected String determineTargetUrl(Authentication authentication) {
        String url = "";

        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

        // для одной роли
        GrantedAuthority auth = authorities.iterator().next();
        String role = auth.getAuthority();


        // для нескольких ролей
//        List<String> roles = new ArrayList<String>();
//        for (GrantedAuthority a : authorities) {
//            roles.add(a.getAuthority());
//        }

        switch (role) {
            case "ROLE_USER":
                url = "/user";
                break;
            case "ROLE_DRIVER":
                url = "/driver";
                break;
            case "ROLE_OWNER":
                url = "/owner";
                break;
            case "ROLE_ADMIN":
                url = "/admin";
                break;
            default:
                url = "/accessDenied";
        }
        return url;
    }



    public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
        this.redirectStrategy = redirectStrategy;
    }

    protected RedirectStrategy getRedirectStrategy() {
        return redirectStrategy;
    }
}

在安全配置中:

    @Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

//
//    @Autowired
//    LoginSuccess loginSuccess;

    @Autowired
    CustomSuccessHandler customSuccessHandler;

    @Autowired
    @Qualifier("customUserDetailsService")
    UserDetailsService userDetailsService;

    @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/user/**").hasRole("USER")
                .antMatchers("/driver/**").hasRole("DRIVER")
                .antMatchers("/owner/**").hasRole("OWNER")
                .and().formLogin().loginPage("/login").successHandler(customSuccessHandler)
//                .successHandler(loginSuccess)
                .usernameParameter("ssoId").passwordParameter("password")
                .and().csrf()
                .and().exceptionHandling().accessDeniedPage("/Access_Denied");
//        http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
    }

    @Bean
    public AuthenticationTrustResolver getAuthenticationTrustResolver() {
        return new AuthenticationTrustResolverImpl();
    }


}

也没有帮助。也通过 onAuthenticationSuccess 尝试过 - 也没有用。

log4j 设置:

    log4j.logger.com.team.mvc.configuration.CustomSuccessHandler=INFO, CustomSuccessHandler
log4j.additivity.com.team.mvc.configuration.CustomSuccessHandler=false
log4j.appender.CustomSuccessHandler=org.apache.log4j.RollingFileAppender
log4j.appender.CustomSuccessHandler.File=E:\\LoginSuccess.out
log4j.appender.CustomSuccessHandler.layout=org.apache.log4j.PatternLayout
log4j.appender.CustomSuccessHandler.layout.ConversionPattern=[%p] %d{yyyy-MM-dd hh:mm:ss} %C:%M:%L - %m%n

而当我尝试通过过滤器时,我无法获取安全上下文,getPrincipal 方法抛出了 NullPointException。这次没有异常,但也没有任何内容写入日志。

spring-security
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Mikhail Vaysman
    2020-04-07T23:55:04Z2020-04-07T23:55:04Z

    创建AuthenticationSuccessHandler

    @Component
    public class LoginSuccess implements AuthenticationSuccessHandler {
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
            System.out.println(authentication.getPrincipal());
        }
    }
    

    并在SpringSecurity配置中连接它。如果您需要了解失败的登录尝试,请创建一个AuthenticationFailureHandler。

    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5