小编Mar*_*eau的帖子

Spring Security permitAll()不允许匿名访问

我有一个方法,我想允许匿名和经过身份验证的访问.

我正在使用基于java的配置的Spring Security 3.2.4.

重写的配置方法(在我的自定义配置类中扩展WebSecurityConfigurerAdapter)具有以下http块:

    http
        .addFilterBefore(muiltpartFilter, ChannelProcessingFilter.class)
        .addFilterBefore(cf, ChannelProcessingFilter.class)
        .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
        .authorizeRequests()
            .antMatchers("/ping**")
            .permitAll()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .logoutUrl("/logout")
        .logoutSuccessUrl("/login");
Run Code Online (Sandbox Code Playgroud)

ping请求处理程序和方法位于一个控制器中,该控制器也包含登录处理程序,它没有单独的@PreAuthorize或其他可能导致该问题的注释.

问题是匿名访问被拒绝,用户被重定向到登录页面.

记录调试级别,我看到Spring Security的以下反馈:

[2014-07-11 13:18:04,483] [DEBUG] [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] Secure object: FilterInvocation: URL: /ping; Attributes: [authenticated]
[2014-07-11 13:18:04,483] [DEBUG] [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@6faad796: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffffa64e: RemoteIpAddress: 192.168.2.128; SessionId: 0EF6B13BBA5F00C020FF9C35A6E3FBA9; Granted Authorities: ROLE_ANONYMOUS
[2014-07-11 13:18:04,483] [DEBUG] [org.springframework.security.access.vote.AffirmativeBased] Voter: org.springframework.security.web.access.expression.WebExpressionVoter@123f2882, returned: -1
[2014-07-11 13:18:04,483] [DEBUG] [org.springframework.security.web.access.ExceptionTranslationFilter] Access is denied (user is anonymous); …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-security

48
推荐指数
5
解决办法
6万
查看次数

将间隔映射到较小间隔的算法

我试着搜索,但由于我的问题的性质,我无法找到令人满意的东西.

我的问题如下:我试图将范围从0到2000(尽管理想情况下上限可调)的数字映射到范围从10到100的小得多的区间.上限将映射(2000-> 100)和也是下限.除此之外,一个比区间[0; 2000]中的另一个条目更大的条目理想地大于[0; 100]中的映射条目

我认为这个问题不是特定于语言的,但如果您想知道,我今天正在使用Javascript.

mapping algorithm distribution

17
推荐指数
3
解决办法
1万
查看次数

使用Spring Web MVC自定义处理405错误

在我的应用程序中,我有一些只允许POST的RequestMappings.如果有人碰巧在该特定路径上发出GET请求,则会收到容器(Tomcat)提供的405错误页面.我可以在web.xml中创建并定义405错误页面以进行自定义.

我想要的:任何导致405错误的请求都应该在特定的控制器方法中处理.

我尝试过的:

  • 一种方法,其中"method = GET"作为所提到的每个映射的对应物.这工作正常,但要求我为每个只允许POST的路径创建一个实际的请求映射和方法.我发现这种不必要的重复和混乱.
  • 一个全局'catch'方法(requestmapping/*):这不起作用,因为Spring将GET方法作为对仅用POST指定的路径的错误调用
  • 一个ExceptionHandler-annotated方法来处理类HttpRequestMethodNotSupportedException的异常:这不起作用.似乎Spring在其框架代码中完全抛出并捕获此异常.
  • 在web.xml中指定我自己的405.这并不完美,因为我想要自定义处理而不是静态错误页面.

java spring spring-mvc

11
推荐指数
2
解决办法
1万
查看次数

Spring HandlerInterceptorAdapter postHandle有保证吗?

我目前有两个处理类似功能的拦截器.我想合并这些.

一个拦截器是访问请求记录器,它显示登录用户,会话ID和请求的URL.

另一个拦截器是一个进程时间记录器.

访问记录器为了记录必须记录的所有记录,请在preHandle方法中记录请求.我们的想法是,不管之后发生了什么(例如,例外),确切的访问请求都会存在.

但是,由于其性质,进程时间记录器必须登录postHandle方法.

为了合并这个功能,我必须将所有内容都移动到一个postHandle方法中.但是,如果在某处发生异常,我可能会丢失一些日志记录,尤其是在应用程序代码中尚未正确处理的异常.

这些考虑是否有任何保证或说明?

java logging spring spring-mvc

7
推荐指数
1
解决办法
7858
查看次数

从persistence.xml引用maven依赖jar的artifactId

我们正在使用JPA,并且正在努力坚持标准规范(并避免使用特定于Hibernate的功能).

我们在另一个项目(A)中使用一个项目(让我们称之为X)作为Maven依赖项.

我们需要JPA来扫描项目X的实体以及扫描项目A.

为此,我们增加了一条线

<jar-file>lib/X-v5-4.0.jar</jar-file>
Run Code Online (Sandbox Code Playgroud)

<persistence-unit>
Run Code Online (Sandbox Code Playgroud)

在persistence.xml中.这很好用.

我们仍然存在的问题是,我们现在需要不仅在pom.xml中而且在persistence.xml中指定项目X的版本.这是未来部署问题的一个方法.

我们已经提出了一个使用Maven资源过滤的系统:

<jar-file>lib/X-v5-${x-version}.jar</jar-file>
Run Code Online (Sandbox Code Playgroud)

在persistence.xml和

<properties>
  <x-version>4.0</x-version>
</properties>
Run Code Online (Sandbox Code Playgroud)

和pom.xml中的$ {x-version}.

这可行,但仍然不完美,因为我们必须记住每次项目X获得新版本时更新非标准位置的版本号.

理想情况下,我们希望我们可以调整pom.xml的依赖部分中的版本信息,并且更改会自动传播到persistence.xml.我们将来通过这种方式减少很多可能的错误.

这可能吗?

编辑(我们的解决方案):

我们添加了一个名为jpa.xml的文件.我们定义了一个entityManagerFactory,一个persistenceAnnotationBeanPostProcessor和一个transactionManager.这里的重要部分是entityManagerFactory bean.它有一个属性"packagesToScan",允许您指示要扫描实体的特定包以放入持久性上下文中.

代码段:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="jpaDataSource" />
    <property name="loadTimeWeaver">
        <bean
            class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
    <property name="packagesToScan">
        <list>
            <value>org.com.our.external.library.package1</value>
            <value>org.com.our.external.library.package2</value>
            <value>org.com.our.external.library.package3</value>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

我相信你会看到优势:因为我们通过包签名来引用这些库,所以我们不再需要担心jar版本号.

hibernate jpa pom.xml maven

5
推荐指数
1
解决办法
2988
查看次数