// Path: src/main/java/security/filter/TokenBasedAuthenticationFilter.java
// public class TokenBasedAuthenticationFilter extends
// 		AbstractAuthenticationProcessingFilter {
// 
// 	protected final Log logger = LogFactory.getLog(getClass());
// 
// 	private final String TOKEN_FILTER_APPLIED = "TOKEN_FILTER_APPLIED";
// 	private AuthTokenGeneratorService authTokenGeneratorService;
// 	private AuthTokenService authTokenService;
// 
// 	public TokenBasedAuthenticationFilter(String defaultFilterProcessesUrl,
// 			AuthTokenGeneratorService authTokenGeneratorService,
// 			AuthTokenService authTokenService) {
// 		super(defaultFilterProcessesUrl);
// 		super.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher(
// 				defaultFilterProcessesUrl));
// 		super.setAuthenticationManager(new NoOpAuthenticationManager());
// 		setAuthenticationSuccessHandler(new TokenBasedAuthenticationSuccessHandlerImpl());
// 		this.authTokenGeneratorService = authTokenGeneratorService;
// 		this.authTokenService = authTokenService;
// 	}
// 
// 	@Override
// 	public Authentication attemptAuthentication(HttpServletRequest request,
// 			HttpServletResponse arg1) throws AuthenticationException,
// 			IOException, ServletException {
// 
// 		AbstractAuthenticationToken userAuthenticationToken = null;
// 		request.setAttribute(TOKEN_FILTER_APPLIED, Boolean.TRUE);
// 
// 		String token = request.getHeader(Constant.HEADER_SECURITY_TOKEN);
// 		userAuthenticationToken = authenticateByToken(token);
// 		if (userAuthenticationToken == null)
// 			throw new AuthenticationServiceException("Bad Token");
// 
// 		return userAuthenticationToken;
// 	}
// 
// 	/**
// 	 * authenticate the user based on token
// 	 * 
// 	 * @return
// 	 */
// 	private AbstractAuthenticationToken authenticateByToken(String token) {
// 		if (null == token) {
// 			return null;
// 		}
// 
// 		AbstractAuthenticationToken authToken = null;
// 
// 		try {
// 			String[] tokens = authTokenGeneratorService.decode(token);
// 
// 			AuthToken tokenEntry = authTokenService.findUserByTokenAndSeries(
// 					tokens[0], tokens[1]);
// 			if (null == tokenEntry) {
// 				return null;
// 			}
// 
// 			security.bean.User securityUser = new security.bean.User(
// 					tokenEntry.getUser());
// 
// 			authToken = new UsernamePasswordAuthenticationToken(
// 					securityUser.getUsername(), "",
// 					securityUser.getAuthorities());
// 		} catch (Exception ex) {
// 			logger.error("Failed to authenticate user for token" + token
// 					+ "{ }", ex);
// 		}
// 
// 		return authToken;
// 	}
// 
// 	@Override
// 	public void doFilter(ServletRequest arg0, ServletResponse arg1,
// 			FilterChain arg2) throws IOException, ServletException {
// 
// 		HttpServletRequest request = (HttpServletRequest) arg0;
// 		HttpServletResponse response = (HttpServletResponse) arg1;
// 
// 		if (request.getAttribute(TOKEN_FILTER_APPLIED) != null) {
// 			arg2.doFilter(request, response);
// 		} else {
// 			super.doFilter(arg0, arg1, arg2);
// 		}
// 
// 	}
// 
// }
// 
// Path: src/main/java/security/service/base/AuthTokenGeneratorService.java
// public interface AuthTokenGeneratorService {
// 
// 	String generateToken(Authentication authentication);
// 
// 	public String[] decode(String token);
// }
// 
// Path: src/main/java/security/service/base/AuthTokenService.java
// public interface AuthTokenService {
// 
// 	AuthToken create(AuthToken authToken);
// 
// 	AuthToken update(AuthToken authToken);
// 
// 	AuthToken findUserByTokenAndSeries(final String token, final String series);
// 
// 	void deleteByTokenAndSeries(final String token, final String series);
// 
// 	void deleteExpiredTokens();
// }


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import security.filter.TokenBasedAuthenticationFilter;
import security.service.base.AuthTokenGeneratorService;
import security.service.base.AuthTokenService;
package security.config;



/**
 * @author abidk
 *
 */
@Configuration
@EnableWebMvcSecurity
@Order(2)
public class APISecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	private AuthTokenGeneratorService authTokenGeneratorService;

	@Autowired
// Path: src/main/java/security/filter/TokenBasedAuthenticationFilter.java
// public class TokenBasedAuthenticationFilter extends
// 		AbstractAuthenticationProcessingFilter {
// 
// 	protected final Log logger = LogFactory.getLog(getClass());
// 
// 	private final String TOKEN_FILTER_APPLIED = "TOKEN_FILTER_APPLIED";
// 	private AuthTokenGeneratorService authTokenGeneratorService;
// 	private AuthTokenService authTokenService;
// 
// 	public TokenBasedAuthenticationFilter(String defaultFilterProcessesUrl,
// 			AuthTokenGeneratorService authTokenGeneratorService,
// 			AuthTokenService authTokenService) {
// 		super(defaultFilterProcessesUrl);
// 		super.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher(
// 				defaultFilterProcessesUrl));
// 		super.setAuthenticationManager(new NoOpAuthenticationManager());
// 		setAuthenticationSuccessHandler(new TokenBasedAuthenticationSuccessHandlerImpl());
// 		this.authTokenGeneratorService = authTokenGeneratorService;
// 		this.authTokenService = authTokenService;
// 	}
// 
// 	@Override
// 	public Authentication attemptAuthentication(HttpServletRequest request,
// 			HttpServletResponse arg1) throws AuthenticationException,
// 			IOException, ServletException {
// 
// 		AbstractAuthenticationToken userAuthenticationToken = null;
// 		request.setAttribute(TOKEN_FILTER_APPLIED, Boolean.TRUE);
// 
// 		String token = request.getHeader(Constant.HEADER_SECURITY_TOKEN);
// 		userAuthenticationToken = authenticateByToken(token);
// 		if (userAuthenticationToken == null)
// 			throw new AuthenticationServiceException("Bad Token");
// 
// 		return userAuthenticationToken;
// 	}
// 
// 	/**
// 	 * authenticate the user based on token
// 	 * 
// 	 * @return
// 	 */
// 	private AbstractAuthenticationToken authenticateByToken(String token) {
// 		if (null == token) {
// 			return null;
// 		}
// 
// 		AbstractAuthenticationToken authToken = null;
// 
// 		try {
// 			String[] tokens = authTokenGeneratorService.decode(token);
// 
// 			AuthToken tokenEntry = authTokenService.findUserByTokenAndSeries(
// 					tokens[0], tokens[1]);
// 			if (null == tokenEntry) {
// 				return null;
// 			}
// 
// 			security.bean.User securityUser = new security.bean.User(
// 					tokenEntry.getUser());
// 
// 			authToken = new UsernamePasswordAuthenticationToken(
// 					securityUser.getUsername(), "",
// 					securityUser.getAuthorities());
// 		} catch (Exception ex) {
// 			logger.error("Failed to authenticate user for token" + token
// 					+ "{ }", ex);
// 		}
// 
// 		return authToken;
// 	}
// 
// 	@Override
// 	public void doFilter(ServletRequest arg0, ServletResponse arg1,
// 			FilterChain arg2) throws IOException, ServletException {
// 
// 		HttpServletRequest request = (HttpServletRequest) arg0;
// 		HttpServletResponse response = (HttpServletResponse) arg1;
// 
// 		if (request.getAttribute(TOKEN_FILTER_APPLIED) != null) {
// 			arg2.doFilter(request, response);
// 		} else {
// 			super.doFilter(arg0, arg1, arg2);
// 		}
// 
// 	}
// 
// }
// 
// Path: src/main/java/security/service/base/AuthTokenGeneratorService.java
// public interface AuthTokenGeneratorService {
// 
// 	String generateToken(Authentication authentication);
// 
// 	public String[] decode(String token);
// }
// 
// Path: src/main/java/security/service/base/AuthTokenService.java
// public interface AuthTokenService {
// 
// 	AuthToken create(AuthToken authToken);
// 
// 	AuthToken update(AuthToken authToken);
// 
// 	AuthToken findUserByTokenAndSeries(final String token, final String series);
// 
// 	void deleteByTokenAndSeries(final String token, final String series);
// 
// 	void deleteExpiredTokens();
// }


// Path: src/main/java/security/config/APISecurityConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import security.filter.TokenBasedAuthenticationFilter;
import security.service.base.AuthTokenGeneratorService;
import security.service.base.AuthTokenService;

package security.config;



/**
 * @author abidk
 *
 */
@Configuration
@EnableWebMvcSecurity
@Order(2)
public class APISecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	private AuthTokenGeneratorService authTokenGeneratorService;

	@Autowired
	private AuthTokenService authTokenService;