<commit_before>package mingzuozhibi.action;

import mingzuozhibi.persist.AutoLogin;
import mingzuozhibi.persist.User;
import mingzuozhibi.support.Dao;
import mingzuozhibi.support.JsonArg;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
public class SessionController extends BaseController {

    public static final Set<GrantedAuthority> GUEST_AUTHORITIES = Stream.of("NONE")
            .map(SimpleGrantedAuthority::new).collect(Collectors.toSet());

    @Autowired
    private Dao dao;

    @Autowired
    private UserDetailsService userDetailsService;

    @GetMapping(value = "/api/session", produces = MEDIA_TYPE)
    public String sessionQuery() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();

        if (!isLogged(authentication)) {
            if (checkAutoLogin()) {

                String username = authentication.getName();
                UserDetails userDetails = userDetailsService.loadUserByUsername(username);
                doLoginSuccess(userDetails.getPassword(), userDetails);
                onLoginSuccess(username, false);

                if (LOGGER.isInfoEnabled()) {
                    infoRequest("[用户自动登入: 已成功自动登入][username={}]", username);
                }
            } else {
                getAttributes().getResponse().addHeader("X-AUTO-LOGIN", "");
            }
        }
        
        JSONObject session = buildSession();
        if (LOGGER.isDebugEnabled()) {
            debugRequest("[获取当前登入状态: session={}]", session);
        }
        return objectResult(session);
    }

    private boolean checkAutoLogin() {
        String header = getAttributes().getRequest().getHeader("X-AUTO-LOGIN");
        if (header == null || header.isEmpty()) {
            if (LOGGER.isDebugEnabled()) {
                debugRequest("[用户自动登入: 未发现有效的X-AUTO-LOGIN]");
            }
            return false;
        }

        if (header.length() != 36) {
            if (LOGGER.isWarnEnabled()) {
                warnRequest("[用户自动登入: 异常的X-AUTO-LOGIN][token={}]", header);
            }
            return false;<commit_msg>style: Remove duplicate blank lines<commit_after>package mingzuozhibi.action;

import mingzuozhibi.persist.AutoLogin;
import mingzuozhibi.persist.User;
import mingzuozhibi.support.Dao;
import mingzuozhibi.support.JsonArg;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
public class SessionController extends BaseController {

    public static final Set<GrantedAuthority> GUEST_AUTHORITIES = Stream.of("NONE")
            .map(SimpleGrantedAuthority::new).collect(Collectors.toSet());

    @Autowired
    private Dao dao;

    @Autowired
    private UserDetailsService userDetailsService;

    @GetMapping(value = "/api/session", produces = MEDIA_TYPE)
    public String sessionQuery() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();

        if (!isLogged(authentication)) {
            if (checkAutoLogin()) {

                String username = authentication.getName();
                UserDetails userDetails = userDetailsService.loadUserByUsername(username);
                doLoginSuccess(userDetails.getPassword(), userDetails);
                onLoginSuccess(username, false);

                if (LOGGER.isInfoEnabled()) {
                    infoRequest("[用户自动登入: 已成功自动登入][username={}]", username);
                }
            } else {
                getAttributes().getResponse().addHeader("X-AUTO-LOGIN", "");
            }
        }

        JSONObject session = buildSession();
        if (LOGGER.isDebugEnabled()) {
            debugRequest("[获取当前登入状态: session={}]", session);
        }
        return objectResult(session);
    }

    private boolean checkAutoLogin() {
        String header = getAttributes().getRequest().getHeader("X-AUTO-LOGIN");
        if (header == null || header.isEmpty()) {
            if (LOGGER.isDebugEnabled()) {
                debugRequest("[用户自动登入: 未发现有效的X-AUTO-LOGIN]");
            }
            return false;
        }

        if (header.length() != 36) {
            if (LOGGER.isWarnEnabled()) {
                warnRequest("[用户自动登入: 异常的X-AUTO-LOGIN][token={}]", header);
            }
            return false;
{'fraction_non_alphanumeric': 0.1049618320610687, 'fraction_numerical': 0.0005871990604815032, 'mean_word_length': 3.5089344804765057, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6268777', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1818, 'n_words': 297}