与之相反:如何使用spring security手动注销用户?
在我的应用程序中,我已经注册了新用户屏幕,该屏幕发布到控制器,该控制器在db中创建新用户(并进行一些明显的检查).然后我希望这个新用户自动登录...我有点想要一些东西像这样 :
SecurityContextHolder.getContext().setPrincipal(MyNewUser);
Run Code Online (Sandbox Code Playgroud)
编辑 好我几乎已经基于如何以Spring Security 3.1以编程方式登录用户的答案实现
Authentication auth = new UsernamePasswordAuthenticationToken(MyNewUser, null);
SecurityContextHolder.getContext().setPrincipal(MyNewUser);
Run Code Online (Sandbox Code Playgroud)
但是,在部署时,jsp无法访问我,MyNewUser.getWhateverMethods()而在正常登录过程之后也是如此.这个代码在名义上有效,但在登录时会抛出错误如下:
<sec:authentication property="principal.firstname" />
Run Code Online (Sandbox Code Playgroud) 我正在将Wicket与Wicket Auth项目一起用于我的表示层,因此我将其与Spring Security集成在一起.这是Wicket为我进行身份验证调用的方法:
@Override
public boolean authenticate(String username, String password) {
try {
Authentication request = new UsernamePasswordAuthenticationToken(
username, password);
Authentication result = authenticationManager.authenticate(request);
SecurityContextHolder.getContext().setAuthentication(result);
} catch (AuthenticationException e) {
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我的Spring Security XML配置的内容(内部)是:
<http path-type="regex">
<form-login login-page="/signin"/>
<logout logout-url="/logout" />
</http>
<global-method-security secured-annotations="enabled" />
<authentication-manager alias="authenticationManager"/>
<authentication-provider user-service-ref="userService">
<password-encoder ref="bcryptpasswordencoder" />
</authentication-provider>
Run Code Online (Sandbox Code Playgroud)
会话固定攻击是潜在的风险,恶意攻击者可以通过访问站点创建会话,然后说服其他用户使用相同的会话登录(通过向他们发送包含会话标识符作为参数的链接,例).Spring Security通过在用户登录时创建新会话来自动防止这种情况.如果您不需要此保护,或者它与其他一些要求冲突,您可以使用session-fixation-protection属性控制行为,有三种选择:
- migrateSession - 创建新会话并将现有会话属性复制到新会话.这是默认值.
- none - 什么都不做.原始会话将保留.
- newSession - 创建新的"干净"会话,而不复制现有的会话数据.
身份验证有效,但我是Spring Security的新手,我还有一些问题需要解答:
我正在尝试将vaadin 10与spring security(使用vaadin提供的spring项目库)集成,我对它们如何完全交互感到困惑.如果我转到受保护的URL(在此示例中,"/ about")直接在浏览器中键入,则会显示登录页面.如果我通过单击UI中的链接转到相同的URL,即使我未经过身份验证,该页面也会显示.所以我猜Vaadin没有通过Spring Security的过滤器链,但是我如何在UI中保护我的资源,以及如何在vaadin和spring之间共享经过身份验证的用户呢?我应该两次实施安全措施吗?可用的文档似乎没有涵盖这一点,互联网上的每个链接都有Vaadin 7-8的例子,我从未使用过,并且似乎与10+不同.
有没有人知道关于这个的任何资源,或者你能告诉我所有这些如何一起工作所以我可以知道我在做什么?
这是我的安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private static final String[] ALLOWED_GET_URLS = {
"/",
//"/about",
"/login/**",
"/frontend/**",
"/VAADIN/**",
"/favicon.ico"
};
private static final String[] ALLOWED_POST_URLS = {
"/"
};
//@formatter:off
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.mvcMatchers(HttpMethod.GET, ALLOWED_GET_URLS)
.permitAll()
.mvcMatchers(HttpMethod.POST, ALLOWED_POST_URLS)
.permitAll()
.anyRequest()
.fullyAuthenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/")
.permitAll();
}
//@formatter:on
}
Run Code Online (Sandbox Code Playgroud)