首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >始终-使用-默认-目标=“false”和默认目标-url不适用于spring-social。

始终-使用-默认-目标=“false”和默认目标-url不适用于spring-social。
EN

Stack Overflow用户
提问于 2013-04-30 21:38:14
回答 1查看 3.4K关注 0票数 2

如果用户使用用户名和密码登录,始终使用默认目标和始终使用默认目标=“false”可以正常工作。

在使用spring-social时,这两个属性似乎都被忽略了。

当用户使用Facebook或Twitter登录时:

  • 如果用户因为单击“登录”按钮而进入登录页面,则在成功登录后,他/她将被重定向到"/“。我预计他/她会被重定向到默认目标url。
  • 如果用户由于试图访问受保护的url而被重定向到登录页面,则在成功登录后,他/她也被重定向到"/“。我预期他/她将被重定向到他/她所要求的原始受保护网址。

我在用

  • 弹簧3.1.3
  • 春季安全3.1.3.
  • 春季社会1.0.2

这是我的Springsecurity.xml

代码语言:javascript
复制
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/security 
                    http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <http use-expressions="true" access-denied-page="/ingresar/?acceso_denegado=true">
        <intercept-url pattern="/" access="permitAll" />
        <intercept-url pattern="/signin/**" access="permitAll" />
        <intercept-url pattern="/url1/**" access="permitAll" />
        <intercept-url pattern="/url2/**" access="hasAnyRole('ROLE_XXX')"/>
        ...
        <intercept-url pattern="/**" access="denyAll" />
        <form-login login-page="/url3/" default-target-url="/url4" always-use-default-target="false" 
                    authentication-failure-url="/url5" login-processing-url="/url6"/>
        <logout logout-url="/logout"/>
    </http>    

    <beans:bean id="myUserService" class="my.kalos.service.MyUserServiceImpl"/>

    <beans:bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"/>

    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref='myUserService'>
            <password-encoder ref="encoder"/>
        </authentication-provider>
    </authentication-manager>
</beans:beans>  

这是我的spring-social配置类。

代码语言:javascript
复制
@Configuration  
public class MyAppSocialConfig {

    @Inject
    MyAppConnectionSignUp myAppConnectionSignUp;

    @Inject
    private DataSource dataSource;

    @Bean
    public ConnectionFactoryLocator connectionFactoryLocator() {
        ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
        registry.addConnectionFactory(new FacebookConnectionFactory(myAppConf.getFbAppId(), myAppConf.getFbAppSecret()));
        registry.addConnectionFactory(new TwitterConnectionFactory(myAppConf.getTtConsumerKey(), myAppConf.getTtConsumerSecret()));
        return registry;
    }

    @Bean
    public UsersConnectionRepository usersConnectionRepository() {
        JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource,
                connectionFactoryLocator(), Encryptors.noOpText());
        repository.setConnectionSignUp(myAppConnectionSignUp);
        return repository;
    }

    @Bean
    @Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
    public ConnectionRepository connectionRepository() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        MyUser user = (MyUser) authentication.getPrincipal();
      return usersConnectionRepository().createConnectionRepository(String.valueOf(user.getId()));
    }

    @Bean
    @Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)   
    public Facebook facebook() {
        return connectionRepository().getPrimaryConnection(Facebook.class).getApi();
    }

    @Bean
    @Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)   
    public Twitter twitter() {
        return connectionRepository().getPrimaryConnection(Twitter.class).getApi();
    }

    @Bean
    public ProviderSignInController providerSignInController() {
        ProviderSignInController controller = new MyAppProviderSignInController(...);
        controller.setSignInUrl("/someUrl/");
        return controller;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-05-13 12:02:13

我终于让它开始工作了,但是我使用了XML配置。这样做的目的是使用postLoginUrl属性覆盖默认的URL ("/")。

代码语言:javascript
复制
<http auto-config="true" use-expressions="true">
    <!-- Enable csrf protection -->
    <csrf />
    <form-login login-page="/sign" default-target-url="/dashboard" authentication-failure-url="/sign" username-parameter="username" password-parameter="password" />
    <!-- Dashboard is protected -->
    <intercept-url pattern="/dashboard**/**" access="hasRole('ROLE_USER')" />
    <!-- Adds social authentication filter to the Spring Security filter chain. -->
    <custom-filter ref="socialAuthenticationFilter" before="PRE_AUTH_FILTER" />
</http>


<!-- Configures the social authentication filter which integrates Spring Social with Spring Security -->
<beans:bean id="socialAuthenticationFilter" class="org.springframework.social.security.SocialAuthenticationFilter">
    <beans:constructor-arg index="0" ref="authenticationManager" />
    <beans:constructor-arg index="1" ref="userIdSource" />
    <beans:constructor-arg index="2" ref="usersConnectionRepository" />
    <beans:constructor-arg index="3" ref="connectionFactoryLocator" />
    <!-- Sets the url of the registration - use in case the sign in has failed -->
    <beans:property name="signupUrl" value="/user/register/" />
    <!-- Sets the url of the dashboard - use in case the sign in has succeed -->
    <beans:property name="postLoginUrl" value="/dashboard/" />
</beans:bean>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16309390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档