use of javax.servlet.Filter in project druid by druid-io.
the class BasicHTTPAuthenticatorTest method testGoodPassword.
@Test
public void testGoodPassword() throws IOException, ServletException {
String header = StringUtils.utf8Base64("userA:helloworld");
header = StringUtils.format("Basic %s", header);
HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
EasyMock.expect(req.getHeader("Authorization")).andReturn(header);
req.setAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT, new AuthenticationResult("userA", "basic", "basic", null));
EasyMock.expectLastCall().times(1);
EasyMock.replay(req);
HttpServletResponse resp = EasyMock.createMock(HttpServletResponse.class);
EasyMock.replay(resp);
FilterChain filterChain = EasyMock.createMock(FilterChain.class);
filterChain.doFilter(req, resp);
EasyMock.expectLastCall().times(1);
EasyMock.replay(filterChain);
Filter authenticatorFilter = AUTHENTICATOR.getFilter();
authenticatorFilter.doFilter(req, resp, filterChain);
EasyMock.verify(req, resp, filterChain);
}
use of javax.servlet.Filter in project druid by druid-io.
the class BasicHTTPAuthenticatorTest method testGoodPasswordWithValidator.
@Test
public void testGoodPasswordWithValidator() throws IOException, ServletException {
CredentialsValidator validator = EasyMock.createMock(CredentialsValidator.class);
BasicHTTPAuthenticator authenticatorWithValidator = new BasicHTTPAuthenticator(CACHE_MANAGER_PROVIDER, "basic", "basic", null, null, false, null, null, false, validator);
String header = StringUtils.utf8Base64("userA:helloworld");
header = StringUtils.format("Basic %s", header);
EasyMock.expect(validator.validateCredentials(EasyMock.eq("basic"), EasyMock.eq("basic"), EasyMock.eq("userA"), EasyMock.aryEq("helloworld".toCharArray()))).andReturn(new AuthenticationResult("userA", "basic", "basic", null)).times(1);
EasyMock.replay(validator);
HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
EasyMock.expect(req.getHeader("Authorization")).andReturn(header);
req.setAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT, new AuthenticationResult("userA", "basic", "basic", null));
EasyMock.expectLastCall().times(1);
EasyMock.replay(req);
HttpServletResponse resp = EasyMock.createMock(HttpServletResponse.class);
EasyMock.replay(resp);
FilterChain filterChain = EasyMock.createMock(FilterChain.class);
filterChain.doFilter(req, resp);
EasyMock.expectLastCall().times(1);
EasyMock.replay(filterChain);
Filter authenticatorFilter = authenticatorWithValidator.getFilter();
authenticatorFilter.doFilter(req, resp, filterChain);
EasyMock.verify(req, resp, validator, filterChain);
}
use of javax.servlet.Filter in project druid by druid-io.
the class BasicHTTPAuthenticatorTest method testBadPasswordWithSkipOnFailureValidator.
@Test
public void testBadPasswordWithSkipOnFailureValidator() throws IOException, ServletException {
CredentialsValidator validator = EasyMock.createMock(CredentialsValidator.class);
BasicHTTPAuthenticator authenticatorWithValidator = new BasicHTTPAuthenticator(CACHE_MANAGER_PROVIDER, "basic", "basic", null, null, false, null, null, true, validator);
String header = StringUtils.utf8Base64("userA:badpassword");
header = StringUtils.format("Basic %s", header);
EasyMock.expect(validator.validateCredentials(EasyMock.eq("basic"), EasyMock.eq("basic"), EasyMock.eq("userA"), EasyMock.aryEq("badpassword".toCharArray()))).andThrow(new BasicSecurityAuthenticationException("User authentication failed.")).times(1);
EasyMock.replay(validator);
HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
EasyMock.expect(req.getHeader("Authorization")).andReturn(header);
EasyMock.replay(req);
HttpServletResponse resp = EasyMock.createMock(HttpServletResponse.class);
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "User authentication failed.");
EasyMock.expectLastCall().times(1);
EasyMock.replay(resp);
// Authentication filter should not move on to the next filter in the chain
FilterChain filterChain = EasyMock.createMock(FilterChain.class);
EasyMock.replay(filterChain);
Filter authenticatorFilter = authenticatorWithValidator.getFilter();
authenticatorFilter.doFilter(req, resp, filterChain);
EasyMock.verify(req, resp, validator, filterChain);
}
use of javax.servlet.Filter in project druid by druid-io.
the class BasicHTTPAuthenticatorTest method testUnknownUserWithSkipOnFailure.
@Test
public void testUnknownUserWithSkipOnFailure() throws IOException, ServletException {
CredentialsValidator validator = EasyMock.createMock(CredentialsValidator.class);
BasicHTTPAuthenticator authenticatorWithSkipOnFailure = new BasicHTTPAuthenticator(CACHE_MANAGER_PROVIDER, "basic", "basic", null, null, false, null, null, true, validator);
String header = StringUtils.utf8Base64("userB:helloworld");
header = StringUtils.format("Basic %s", header);
HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
EasyMock.expect(req.getHeader("Authorization")).andReturn(header);
EasyMock.replay(req);
EasyMock.expect(validator.validateCredentials(EasyMock.eq("basic"), EasyMock.eq("basic"), EasyMock.eq("userB"), EasyMock.aryEq("helloworld".toCharArray()))).andReturn(null).times(1);
EasyMock.replay(validator);
HttpServletResponse resp = EasyMock.createMock(HttpServletResponse.class);
EasyMock.replay(resp);
// Authentication filter should move on to the next filter in the chain without sending a response
FilterChain filterChain = EasyMock.createMock(FilterChain.class);
filterChain.doFilter(req, resp);
EasyMock.expectLastCall().times(1);
EasyMock.replay(filterChain);
Filter authenticatorFilter = authenticatorWithSkipOnFailure.getFilter();
authenticatorFilter.doFilter(req, resp, filterChain);
EasyMock.verify(req, resp, validator, filterChain);
}
use of javax.servlet.Filter in project wechat by dllwh.
the class ShiroConfig method shiroFilter.
/**
* ----------------------------------------------------- Fields end
*/
/**
* @方法描述 :
*
* <pre>
* Shiro主过滤器本身功能十分强大,其强大之处就在于它支持任何基于URL路径表达式的、自定义的过滤器的执行
* Web应用中,Shiro可控制的Web请求必须经过Shiro主过滤器的拦截,Shiro对基于Spring的Web应用提供了完美的支持
* </pre>
*
* @return
*/
// @Bean("shiroFilter")
public ShiroFilterFactoryBean shiroFilter() {
ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean();
// Shiro的核心安全接口,这个属性是必须的
shiroFilterFactory.setSecurityManager(securityManager());
// 要求登录时的链接(可根据项目的URL进行替换),非必须的属性,如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
shiroFilterFactory.setLoginUrl(FilterHelper.LOGIN_ACTION);
// 登录成功后要跳转的链接
shiroFilterFactory.setSuccessUrl(FilterHelper.LOGIN_ACTION);
// 用户访问未对其授权的资源时,所显示的连接;
shiroFilterFactory.setUnauthorizedUrl(FilterHelper.UNAUTHORIZED);
/**
* 自定义拦截器
*/
Map<String, Filter> filtersMap = new LinkedHashMap<String, Filter>();
// 登录校验
filtersMap.put("loginFilter", new LoginFilter());
// 角色判断校验
filtersMap.put("roleFilter", new RoleFilter());
// 权限校验
filtersMap.put("permissionFilter", new PermissionFilter());
filtersMap.put("kickoutFilter", kickoutSessionFilter());
// 用户session
filtersMap.put("userSessionFilter", new UserSessionFilter());
shiroFilterFactory.setFilters(filtersMap);
/**
* Shiro连接约束配置,即权限控制map.
* 第一个'/'代表的路径是相对于HttpServletRequest.getContextPath()的值来的
*/
Map<String, String> filterMap = new LinkedHashMap<String, String>();
filterMap.put("/loginController**", "anon");
filterMap.put("/sysPage/**", "anon,kickoutFilter");
filterMap.put("/homeController/**", "loginFilter");
filterMap.put("/dataSourceController**", "authc,roleFilter[administrator]");
filterMap.put("/**", "authc,loginFilter,permissionFilter,kickoutFilter");
shiroFilterFactory.setFilterChainDefinitionMap(filterMap);
return shiroFilterFactory;
}
Aggregations