security work

This commit is contained in:
eugenp 2013-05-23 23:25:53 +03:00
parent 794e3c0a95
commit e85fd01d61
6 changed files with 78 additions and 27 deletions

View File

@ -0,0 +1,29 @@
package org.baeldung.spring.security;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler {
public CustomLogoutSuccessHandler() {
super();
}
// API
@Override
public void onLogoutSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException, ServletException {
final String refererUrl = request.getHeader("Referer");
System.out.println(refererUrl);
super.onLogoutSuccess(request, response, authentication);
}
}

View File

@ -13,28 +13,30 @@ import org.springframework.web.servlet.view.JstlView;
@Configuration @Configuration
public class ClientWebConfig extends WebMvcConfigurerAdapter { public class ClientWebConfig extends WebMvcConfigurerAdapter {
public ClientWebConfig() { public ClientWebConfig() {
super(); super();
} }
// API // API
@Override @Override
public void addViewControllers(final ViewControllerRegistry registry) { public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry); super.addViewControllers(registry);
registry.addViewController("/login.html"); registry.addViewController("/anonymous.html");
registry.addViewController("/homepage.html");
}
@Bean registry.addViewController("/login.html");
public ViewResolver viewResolver() { registry.addViewController("/homepage.html");
final InternalResourceViewResolver bean = new InternalResourceViewResolver(); }
bean.setViewClass(JstlView.class); @Bean
bean.setPrefix("/WEB-INF/view/"); public ViewResolver viewResolver() {
bean.setSuffix(".jsp"); final InternalResourceViewResolver bean = new InternalResourceViewResolver();
return bean; bean.setViewClass(JstlView.class);
} bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
} }

View File

@ -5,9 +5,14 @@
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<http use-expressions="true"> <debug/>
<intercept-url pattern="/login*" access="permitAll" />
<intercept-url pattern="/**" access="isAuthenticated()" /> <http use-expressions="true" >
<intercept-url pattern="/anonymous*" access="isAnonymous()" />
<intercept-url pattern="/login*" access="permitAll" />
<intercept-url pattern="/**" access="isAuthenticated()" />
<form-login <form-login
login-page='/login.html' login-page='/login.html'
@ -16,10 +21,15 @@
authentication-failure-url="/login.html?error=true" authentication-failure-url="/login.html?error=true"
always-use-default-target="true"/> always-use-default-target="true"/>
<logout/> <logout
logout-url="/perform_logout"
delete-cookies="JSESSIONID"
success-handler-ref="customLogoutSuccessHandler" />
</http> </http>
<beans:bean name="customLogoutSuccessHandler" class="org.baeldung.spring.security.CustomLogoutSuccessHandler" />
<authentication-manager> <authentication-manager>
<authentication-provider> <authentication-provider>
<user-service> <user-service>

View File

@ -0,0 +1,10 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head></head>
<body>
<h1>Anonymous page</h1>
<a href="<c:url value="/login.html" />">To Login</a>
</body>
</html>

View File

@ -4,6 +4,6 @@
<body> <body>
<h1>This is the body of the sample view</h1> <h1>This is the body of the sample view</h1>
<a href="<c:url value="j_spring_security_logout" />"> Logout</a> <a href="<c:url value="/perform_logout" />">Logout</a>
</body> </body>
</html> </html>

View File

@ -45,8 +45,8 @@
<url-pattern>/*</url-pattern> <url-pattern>/*</url-pattern>
</filter-mapping> </filter-mapping>
<welcome-file-list> <!-- <welcome-file-list> -->
<welcome-file>index.html</welcome-file> <!-- <welcome-file>index.html</welcome-file> -->
</welcome-file-list> <!-- </welcome-file-list> -->
</web-app> </web-app>