我在使用Spring Security时遇到一些问题,并且使访问被拒绝的处理程序起作用。
Spring Security可以正常工作,但是当我在没有必需权限(ROLE_ADMIN)的情况下访问/ admin时,Spring Security只是重定向到根页面,这是我的登录表单页面。
我希望能够将用户重定向到/ accessdenied或/?accessdenied = true,这将加载登录页面并显示以下消息:“权限被拒绝-请登录”
spring-security.xml:
<beans:beans
xmlns:security="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<security:http>
<security:intercept-url pattern='/home' access='ROLE_USER,ROLE_ADMIN' />
<security:intercept-url pattern='/admin*' access='ROLE_ADMIN' />
<security:form-login login-page='/' default-target-url='/home' authentication-failure-url='/?error=true' />
<security:logout logout-success-url='/' />
<security:access-denied-handler error-page="/accessdenied"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name='a' password='a' authorities='ROLE_ADMIN,ROLE_USER' />
<security:user name='u' password='u' authorities='ROLE_USER' />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans:beans>
Run Code Online (Sandbox Code Playgroud)
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters …Run Code Online (Sandbox Code Playgroud) 我正在尝试从MongoDB Java驱动程序调用存储的JavaScript函数.
我一直在遵循本指南将该函数存储在DB服务器上,我可以从mongo shell调用该函数并返回结果.
但是我无法弄清楚如何在Java中调用相同的函数?
根据这个http://api.mongodb.org/java/current/com/mongodb/DB.html#doEval-java.lang.String-java.lang.Object ... .-有一个叫做的方法doEval
我也试过用这种方法:
public static String callFunction() {
try (MongoClient client = new MongoClient("localhost")) {
com.mongodb.DB db = client.getDB("TestDB");
return db.doEval("echoFunction", 3).toString();
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我调用这个方法时,这就是我得到的:
{ "retval" : { "$code" : "function (x) {\n return x;\n}"} , "ok" : 1.0}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我希望得到3号.
上述代码的另一个问题是该方法client.getDB()已弃用.据我所知,调用的新方法是client.getDatabase(),它返回一个MongoDatabase对象,但根据API,没有方法来执行一个函数.
所以我的问题是:是否可以从Java在数据库服务器上执行存储的JavaScript函数并获取该函数的结果?如果有可能的话,我会对如何做到这一点表示感谢?
谢谢.
编辑:
根据来自java的mongodb上的calling server js函数的评论:
"似乎getNextSequence是一个用mongo javascript shell编写的函数.数据库(mongod)和Java端都不知道这个函数是否存在,也无法解释函数包含的Javascript代码.你将不得不在Java中重新实现它. ."
我试图实现的函数比上面的例子复杂一点 - 它应该返回一个文档集合,并且似乎没有使用db.doEval方法.
所以我认为评论是正确的?
我的情况是这样的:我需要将基于邮政编码的邮件路由到三个不同的商店.
为此,我需要查看邮件标题以查找客户的邮政编码,并执行以下计算:
if(zip < 5000)
{
store = "SJ";
}
else if(zip >= 6000)
{
store = "JY";
}
else
{
store = "FY";
}
Run Code Online (Sandbox Code Playgroud)
我已经设法使用以下自定义Transformer来完成它,我用它来丰富消息头:
public class HeaderEnricher {
public Message<?> transform(Message<?> message)
{
int zip = message.getHeaders().get("Customer Zip", Integer.class);
String store;
if (zip < 5000)
{
store = "SJ";
}
else if (zip >= 6000)
{
store = "JY";
}
else
{
store = "FY";
}
Message<?> messageOut = MessageBuilder
.withPayload(message.getPayload())
.copyHeadersIfAbsent(message.getHeaders())
.setHeaderIfAbsent("store", store).build();
return messageOut;
}
} …Run Code Online (Sandbox Code Playgroud) 我是Spring Integration和Spring Integration AMQP的新手.
我有以下代码:
<bean id="enricher" class="soft.Enricher"/>
<amqp:inbound-channel-adapter queue-names="QUEUE1" channel="amqpInboundChannel"/>
<int:channel id="amqpInboundChannel">
<int:interceptors>
<int:wire-tap channel="logger"/>
</int:interceptors>
</int:channel>
<int:header-enricher input-channel="amqpInboundChannel" output-channel="routingChannel">
<int:header name="store" value="sj" />
</int:header-enricher>
<int:channel id="routingChannel" />
<int:header-value-router input-channel="routingChannel" header-name="store">
<int:mapping value="sj" channel="channelSJ" />
<int:mapping value="jy" channel="channelJY" />
</int:header-value-router>
<amqp:outbound-channel-adapter channel="channelSJ" exchange-name="ex_store" routing-key="sj" amqp-template="rabbitTemplate"/>
<amqp:outbound-channel-adapter channel="channelJY" exchange-name="ex_store" routing-key="jy" amqp-template="rabbitTemplate"/>
<int:channel id="channelSJ" />
<int:channel id="channelJY" />
<int:logging-channel-adapter id="logger" level="ERROR" />
Run Code Online (Sandbox Code Playgroud)
设置如下:

一切正常,但是当入站通道适配器拾取消息时,标题会丢失.
同样,当使用出站通道适配器将消息发送到交换机时,被称为"存储"的标题也被丢失.
这是消息在被入站通道适配器拾取之前的样子:

这是同一条消息在整个过程中的注意事项(注意没有标题)
