java连接打印机访问被拒绝_java – 尝试访问spring security中的登录页面时访问被拒绝的异常...
我正在使用基于java的spring security.我创建了自定义访问决策选民impl.
但是当我运行应用程序时,我无法打开登录页面,因为它说,访问被拒绝.
这是在我添加自定义访问决策选民impl之后发生的.我想问题是由于自定义AccessDecisionVoter中的以下代码.
if(authentication instanceof AnonymousAuthenticationToken)
return ACCESS_DENIED;
但我需要这样,以便不检查未登录用户的权限.
它进入无限循环,登录页面,访问决策选民,访问被拒绝,登录页面等.
下面是spring安全配置代码.
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private AffirmativeBased accessDecisionManager;
@Bean
@Autowired
public AffirmativeBased accessDecisionManager(AccessDecisionVoterImpl accessDecisionVoter) {
List> accessDecisionVoters = new ArrayList>();
accessDecisionVoters.add(accessDecisionVoter);
AffirmativeBased accessDecisionManager = new AffirmativeBased(accessDecisionVoters);
return accessDecisionManager;
}
@Override
@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder(){
PasswordEncoder passwordEncoder = new PasswordEncoder();
passwordEncoder.setStringDigester(stringDigester());
return passwordEncoder;
}
@Bean
public PooledStringDigester stringDigester() {
PooledStringDigester psd = new PooledStringDigester();
psd.setPoolSize(2);
psd.setAlgorithm("SHA-256");
psd.setIterations(1000);
psd.setSaltSizeBytes(16);
psd.setSaltGenerator(randomSaltGenerator());
return psd;
}
@Bean
public RandomSaltGenerator randomSaltGenerator() {
RandomSaltGenerator randomSaltGenerator = new RandomSaltGenerator();
return randomSaltGenerator;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/static/**")
.antMatchers("/i18n/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/checkLogin")
.defaultSuccessUrl("/home")
.failureUrl("/login?login_error=1")
.usernameParameter("username")
.passwordParameter("password")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login?isLoggedOut=1")
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.permitAll()
.and()
.authorizeRequests()
.antMatchers("/login**").permitAll()
.antMatchers("/error**").permitAll()
.antMatchers("/checkLogin**").permitAll()
.anyRequest()
.authenticated()
.accessDecisionManager(accessDecisionManager)
.and()
.exceptionHandling()
.accessDeniedPage("/accessDenied")
.and()
.headers()
.frameOptions()
.disable()
.and()
.sessionManagement()
.invalidSessionUrl("/login")
.maximumSessions(1);
}
}
和我的自定义选民impl
@Component
public class AccessDecisionVoterImpl implements AccessDecisionVoter {
@Autowired
private ModuleService moduleService;
@Override
public boolean supports(ConfigAttribute attribute) {
return true;
}
@Override
public boolean supports(Class clazz) {
return true;
}
@Override
public int vote(Authentication authentication, Object object, Collection collection) {
// i have given this so that if user is not logged in then should not check permission at all
if(authentication instanceof AnonymousAuthenticationToken)
return ACCESS_DENIED;
HttpServletRequest request = ((FilterInvocation) object).getHttpRequest();
String requestedOperation = request.getParameter("operation");
if (requestedOperation != null && !requestedOperation.isEmpty()){
String [] requestURISplit = request.getRequestURI().split("/");
String requestedModuleName = requestURISplit[2];
if(SecurityUtils.hasPermission(requestedModuleName, requestedOperation)){
return ACCESS_GRANTED;
}
} else {
return ACCESS_GRANTED;
}
return ACCESS_DENIED;
此外,当我从选民中删除以下行时,如果用户未登录并尝试访问受保护的页面,则会继续.它应该已重定向到登录页面.
if(authentication instanceof AnonymousAuthenticationToken)
return ACCESS_DENIED;
这是我第一次尝试使用弹簧靴.因此,我不确定所有的配置问题.
antMatchers的顺序有什么问题吗?
请帮忙.
解决方法:
>弃权投票(AccessDecisionVoter.ACCESS_ABSTAIN):如果选民无法做出决定(例如,用户未经授权,无法从请求上下文获取模块等)
> grant access(AccessDecisionVoter.ACCESS_GRANTED):如果可以识别模块并且用户被授权
>拒绝访问(AccessDecisionVoter.ACCESS_DENIED):如果可以识别模块并且用户未被授权
使用AccessDecisionManager配置,您基本上可以取消基于URL的访问限制,例如:
http.authorizeRequests()
.antMatchers("/css/**", "/img/**", "/js/**", "/font/**").permitAll()
.antMatchers("/login**").permitAll()
.antMatchers("/error**").permitAll()
.antMatchers("/checkLogin**").permitAll()
.anyRequest()
.authenticated()
默认情况下,spring为此目的使用WebExpressionVoter.
但是,如果至少有一个AccessDecisionVoter授予对资源的访问权限,则AffirmativeBased AccessDecisionManager将授予访问权限(这可能不是您想要的).
根据您的要求,包含WebExpressionVoter的ConsensusBased AccessDecisionManager将是最佳匹配.
@Bean
public AccessDecisionManager accessDecisionManager() {
List> decisionVoters = new ArrayList<>();
decisionVoters.add(new WebExpressionVoter());
decisionVoters.add(new ModuleAccessDecisionVoter());
ConsensusBased consensusBased = new ConsensusBased(decisionVoters);
// deny access if voters equally voted to allow and deny access
consensusBased.setAllowIfEqualGrantedDeniedDecisions(false);
return consensusBased;
}
您的AccessDecisionVoter实现:
static class ModuleAccessDecisionVoter implements AccessDecisionVoter {
public int vote(Authentication authentication, FilterInvocation object, Collection attributes) {
if (authentication == null || authentication instanceof AnonymousAuthenticationToken) {
return ACCESS_ABSTAIN;
}
// determine module and grant or deny access
// if module cannot be determined abstain from voting
String module = determineModule(object);
if (module != null) {
return isAccessGranted(module, authentication) ? ACCESS_GRANTED : ACCESS_DENIED
}
return ACCESS_ABSTAIN;
}
}
匿名访问应导致以下结果:
> / login:WebExpressionVoter:1,ModuleVoter:0 – > 1 = ACCESS_GRANTED
> / foo-module:WebExpressionVoter:-1,ModuleVoter:-1 – > -2 = ACCESS_DENIED
给定允许查看Foo模块的用户应该产生以下结果:
> / foo-module:WebExpressionVoter:1,ModuleVoter:1 – > 2 = ACCESS_GRANTED
> / bar-module:WebExpressionVoter:1(因为用户已通过身份验证),ModuleVoter:-1 – > 0 = ACCESS_DENIED(因为ConsensusBased.setAllowIfEqualGrantedDeniedDecisions(false))
java连接打印机访问被拒绝_java – 尝试访问spring security中的登录页面时访问被拒绝的异常...相关推荐
- Java访问静态常量_Java如何在Spring EL中访问静态方法或常量?
在这个例子中,您将学习如何使用Spring Expression Language访问类范围的方法或常量.要访问类范围的方法或常量T(),例如,您将需要使用Spring EL的运算符T(java.la ...
- 从零开始java安全权限框架篇(一):spring security配置登录登出的配置
目录 一:安全权限框架的选取 二:功能 三:登录登出 四:代码注释 1.将登陆交由Spring security完成 2.前台明文密码加密,与数据库比对 3.关键配置 4.自定义用户异常 5.ajax ...
- java 连接打印机实现打印PDF文件的功能
java 连接打印机打印PDF文件 需求背景是导出excel并且打印,excel导出设置和转PDF网上有很多资料,本文只记录连接打印机打印PDF的部分. 1: 需要引入maven配置 <depe ...
- 如何使用Java和XML Config在Spring Security中启用HTTP基本身份验证
在上一篇文章中,我向您展示了如何在Java应用程序中启用Spring安全性 ,今天我们将讨论如何使用Spring Security 在Java Web应用程序中启用Basic HTTP身份验证 . 如 ...
- Spring Security 实战:使用 JWT 认证访问接口
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 1. 前言 欢迎阅读Spring Security 实战 ...
- Spring Security 3 Ajax登录–访问受保护的资源
我看过一些有关Spring Security 3 Ajax登录的博客,但是我找不到解决如何调用基于Ajax的登录的博客,匿名用户正在Ajax中访问受保护的资源. 问题 – Web应用程序允许匿名访问某 ...
- java按钮权限控制_详解Spring Security 中的四种权限控制方式
Spring Security 中对于权限控制默认已经提供了很多了,但是,一个优秀的框架必须具备良好的扩展性,恰好,Spring Security 的扩展性就非常棒,我们既可以使用 Spring Se ...
- java连接MySQL几种方法_Java连接MySQL数据库三种方法
好久没有更新博客了!今天利用周目时学习了一下数据库mysql.介绍一下数据库的三种连接方式! 开发工具:Myeclipse MySQL5.6 MySQL连接驱动:mysql-connector-jav ...
- Windows7中启动Mysql服务时提示:拒绝访问的一种解决方式
场景 在Windows7中打开任务管理器--服务下 找到mysql的服务点击启动时提示: 拒绝访问 这是因为权限不够导致的不能启动sql服务. 点击 任务管理器右下角的服务 在这里就可以正常启动服务
最新文章
- matlab函数isempty_matlab基本函数inf, isempty, round, floor, fix
- jquery实现表格中点击相应行变色功能
- Windows删除文件时显示找不到该项目
- python config配置文件的读写--configparser
- MVC,三层架构,工厂模型,七层
- php启用openssl,php怎么开启openssl模块
- 【技术累积】【点】【java】【29】MapUtils
- Android Studio 引用aar包 更新后找不到新增的方法问题(踩坑)
- bzoj 2908. 又是nand(树链剖分+区间NAND+单点修改)
- 数学建模python教材推荐_数模竞赛专攻python应该准备什么?
- Windows 7 HomeGroup的隐私保护
- MySQL入门之select、from、join、where子句及基本匹配符
- python 仅保留数字_Python基本语法元素 | 语法元素分析
- Gin+Mysql简单的Restful风格的API
- m苹果放n篮子_m个苹果放入n个篮子
- 电商订单仓储ERP管理系统软件源码
- 高等数学Mathematica实验题——费马素数猜想(Fn=2^(2^n)+1为素数)的证伪(Verification of Fermat's Prime Number Function)
- iOS 绘图(drawrect)图片裁剪的红色框框
- 2012年中国土地市场网数据(含经纬度)
- 在家也能查sci--漫游登陆web of science