如果用户使用用户名和密码登录,always-use-default-target 和 always-use-default-target="false" 工作正常
使用 spring-social 时,这两个属性似乎都被忽略了
当用户使用 Facebook 或 Twitter 登录时:
- 如果用户是因为点击了“登录”按钮而进入了登录页面,那么在成功登录后他/她将被重定向到“/”。我希望他/她被重定向到 default-target-url
- 如果用户因为试图访问受保护的 url 而被重定向到登录页面,那么在成功登录后他/她也会被重定向到“/”。我希望他/她被重定向到他/她请求的原始受保护 url。
我在用
- 春天 3.1.3.RELEASE
- 春季安全 3.1.3.RELEASE
- 春季社交 1.0.2.RELEASE
这是我的 spring-security.xml
<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 配置类
@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;
}
}