启用S​​pring Social的日志记录

时间:2015-03-04 22:25:19

标签: java spring log4j spring-social spring-social-facebook

我有一个Spring MVC应用程序,它使用Spring Social Facebook来启用Facebook登录。这在我的本地计算机上运行正常,但它目前在我的暂存环境中不起作用。我的问题是我无法弄清楚出了什么问题。当我尝试登录时,我被重定向到Facebook以授权该应用程序。这部分工作正常。但是当我授权该应用时,所有发生的事情都是我被重定向回http://example.com/signin?error=provider#_=_,这会产生404 Not Found错误。

这对于调试错误源并不是很有用,查看我的Tomcat日志文件,我找不到任何可以帮助我找到错误的信息。没有堆栈跟踪,错误消息等。我在我的项目中启用了log4j,但是我没有看到来自Spring Social的任何日志。实际上我不确定如何配置Spring Social的日志记录,但我在 log4j2.xml 文件中尝试了以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>

    <Loggers>
        <Logger name="com.example" level="trace">
            <AppenderRef ref="Console" />
        </Logger>

        <Logger name="org.springframework.social" level="all">
            <AppenderRef ref="Console" />
        </Logger>

        <Logger name="org.springframework.social.facebook" level="all">
            <AppenderRef ref="Console" />
        </Logger>

        <Logger name="org.springframework.social.security" level="all">
            <AppenderRef ref="Console" />
        </Logger>

        <Root level="error">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

但是,我在控制台中看不到任何Spring Social日志。如果我在我的应用程序中写入它,如下所示,日志记录工作正常,因此日志记录应该正常工作。

@Controller
public class IndexController {
    private static Logger logger = LogManager.getLogger(IndexController.class);

    @RequestMapping("/")
    public String home() {
        logger.debug("Hello there from logger!");
        return "home";
    }
}

如何为Spring Social配置日志记录?我发现了一些按照我的方式配置log4j的示例(例如one used for Spring Social tests),但没有其他任何内容。我想库支持这个,因为否则我不知道如何调试错误。或者有没有其他方法可以找出触发错误的内容?

以下是我的Spring Social配置,即使我认为在这种情况下不重要。

@Configuration
public class SocialConfig {
    @Autowired
    private DataSource dataSource;

    @Autowired
    private TextEncryptor textEncryptor;

    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountRepository accountRepository;

    @Value("${facebook.app.id}")
    private String facebookAppId;

    @Value("${facebook.app.secret}")
    private String facebookAppSecret;

    @Bean
    public ProviderSignInController providerSignInController() {
        ProviderSignInController controller = new ProviderSignInController(this.connectionFactoryLocator(), this.usersConnectionRepository(), new SpringSecuritySignInAdapter(this.accountRepository));
        controller.addSignInInterceptor(new RedirectToPreviousPageInterceptor(controller));

        return controller;
    }

    @Bean
    public ConnectionFactoryRegistry connectionFactoryLocator() {
        ConnectionFactoryRegistry connectionFactoryRegistry = new ConnectionFactoryRegistry();
        List<ConnectionFactory<?>> connectionFactories = new ArrayList<ConnectionFactory<?>>();
        connectionFactories.add(this.facebookConnectionFactory());
        connectionFactoryRegistry.setConnectionFactories(connectionFactories);

        return connectionFactoryRegistry;
    }

    @Bean
    public FacebookConnectionFactory facebookConnectionFactory() {
        FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory(this.facebookAppId, this.facebookAppSecret);
        connectionFactory.setScope("email");

        return connectionFactory;
    }

    @Bean
    public JdbcUsersConnectionRepository usersConnectionRepository() {
        JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(this.dataSource, this.connectionFactoryLocator(), this.textEncryptor);
        repository.setConnectionSignUp(this.accountService);

        return repository;
    }
}

非常感谢你。

1 个答案:

答案 0 :(得分:0)

经过大量的试验和错误,我设法解决了这个问题。原来我的项目依赖项不正确,Spring Framework的日志记录没有通过slf4j正确发送到log4j。我使用了以下Maven依赖项:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.2</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.2</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.2</version>
</dependency>

然后我使用问题中发布的log4j2.xml启用了Spring Framework的日志记录,并将其隐藏在十亿日志中,我发现了以下内容:

ERROR org.springframework.social.connect.web.ProviderSignInController - Exception while completing OAuth 2 connection: 
java.lang.IllegalArgumentException: Unable to initialize due to invalid secret key
    at org.springframework.security.crypto.encrypt.CipherUtils.initCipher(CipherUtils.java:110) ~[spring-security-core-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.security.crypto.encrypt.AesBytesEncryptor.encrypt(AesBytesEncryptor.java:65) ~[spring-security-core-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.security.crypto.encrypt.HexEncodingTextEncryptor.encrypt(HexEncodingTextEncryptor.java:36) ~[spring-security-core-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.social.connect.jdbc.JdbcConnectionRepository.encrypt(JdbcConnectionRepository.java:236) ~[spring-social-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
    at org.springframework.social.connect.jdbc.JdbcConnectionRepository.addConnection(JdbcConnectionRepository.java:167) ~[spring-social-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
    at org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository.findUserIdsWithConnection(JdbcUsersConnectionRepository.java:89) ~[spring-social-core-1.1.0.RELEASE.jar:1.1.0.RELEASE]
    at org.springframework.social.connect.web.ProviderSignInController.handleSignIn(ProviderSignInController.java:260) ~[spring-social-web-1.1.0.RELEASE.jar:1.1.0.RELEASE]
    at org.springframework.social.connect.web.ProviderSignInController.oauth2Callback(ProviderSignInController.java:217) [spring-social-web-1.1.0.RELEASE.jar:1.1.0.RELEASE]
    ...
Caused by: java.security.InvalidKeyException: Illegal key size
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1034) ~[?:1.8.0_20]
    at javax.crypto.Cipher.implInit(Cipher.java:800) ~[?:1.8.0_20]
    at javax.crypto.Cipher.chooseProvider(Cipher.java:859) ~[?:1.8.0_20]
    at javax.crypto.Cipher.init(Cipher.java:1370) ~[?:1.8.0_20]
    at javax.crypto.Cipher.init(Cipher.java:1301) ~[?:1.8.0_20]
    at org.springframework.security.crypto.encrypt.CipherUtils.initCipher(CipherUtils.java:105) ~[spring-security-core-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    ... 85 more

正如您所看到的,它是由密钥长度太长引起的,因为JDK由于政治原因,默认情况下仅默认支持128位加密。为了解决这个问题,我遵循了this SO question的建议。我在this excellent blog post的详细信息中对此进行了解释。对于OS X,JAVA_HOME位于/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home,因此您可以将文件粘贴到/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/jre/lib/security。出现此问题是因为我在开发计算机上使用了noOpText文本加密器,当我将应用程序移动到登台环境时,我将应用程序设置为使用querable text encryptor。确保密钥长度不再限制为128位后,我必须将userconnection表的accesstoken列的长度增加到varchar(255)。那就是它!它现在可以正常工作。

我希望这可以帮助处于类似情况的人。