我在Spring 4.2.0.RELEASE中使用Spring Security 4.0.2.RELEASE。使用Java配置注销Spring Security
我无法创建一个注销链接(我maen什么必须是href属性的值)。
考虑:
配置的DelegatingFilterProxy在Java中与WebApplicationInitializer:
public class SecurityWebInitializer
extends AbstractSecurityWebApplicationInitializer {
}
简单的配置类,使网络安全的Spring MVC
@Configuration
@EnableWebSecurity
public class SecurityConfig
extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().and()
.authorizeRequests()
.antMatchers("/spitter/").authenticated()
.antMatchers(HttpMethod.GET, "/spitter/register").authenticated().and()
.logout().deleteCookies("remove")
.invalidateHttpSession(true).logoutUrl("/logout")
.logoutSuccessUrl("/");
}
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password")
.roles("USER").and().withUser("admin").password("password")
.roles("USER", "ADMIN");
}
}
控制器:
@Controller
@RequestMapping(value = "/spitter")
public class SpittrController {
private SpittleRepository spittleRepository;
@Autowired
public SpittrController(SpittleRepository spittleRepository) {
this.spittleRepository = spittleRepository;
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String showRegistrationForm() {
return "registerForm";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String processingRegistration(@Valid Spitter spitter, Errors errors) {
if (errors.hasErrors()) {
return "registerForm";
}
spittleRepository.save(spitter);
return "redirect:/spitter/" + spitter.getUserName();
}
@RequestMapping(value = "/{username}", method = RequestMethod.GET)
public String showSpitterProfile(@PathVariable("username") String username,
Model model) {
Spitter spitter = spittleRepository.findByUsername(username);
if(spitter != null){
model.addAttribute(spitter);
}
return "profile";
}
}
registerForm.jsp:
<form method="post">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="firstName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lastName" /></td>
</tr>
<tr>
<td>User Name:</td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Register" /></td>
</tr>
</table>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
</form>
提交后registerForm.jsp,所述profile.jsp被示出给用户:
profile.jsp:
<body>
<h1>Hello world!</h1>
<p>The time on the server is ${serverTime}.</p>
<h1>Your Profile</h1>
<h1><a href="/logout">Logout</a></h1>
<table>
<tr>
<td>First Name:</td>
<td><c:out value="${spitter.firstName}" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><c:out value="${spitter.lastName}" /></td>
</tr>
<tr>
<td>User Name:</td>
<td><c:out value="${spitter.userName}" /></td>
</tr>
</table>
</body>
当我打
我被重定向到登录页面。登录并提交表格后,将显示profile.jsp,其中包含一个注销链接。在点击,HTTP 404出现。
我已经通过Spring Security docs走了,但他们已经采取了thymeleaf考虑。我是一个简单的JSP页面。
而且,我也认为考虑到这一点,
默认情况下POST请求需要注销的URL。要执行的GET请求 注销您需要:
HTTP .logout() .logoutRequestMatcher(新AntPathRequestMatcher( “/注销”));
1: http://docs.spring.io/spring-security/site/docs/3.2.x/guides/hellomvc.html
有什么建议?
'/ logout'作为一种形式的URL或行动才有效。你需要包含'context-root'。使用该URL的标签为' '应该这样做。 –
@Denium:你的意思是web/spitter /注销。 –
如果'/ web/spitter'是你的应用程序的上下文根,是的,否则不......如上所述,使用URL标签为你做。 (同样在答案中,这基本上是正确的,但包含错误的URL)。 –