我已经使用spring的sparklr示例应用程序和我在网上找到的几个示例实现了spring oauth2的资源所有者流程.我用curl测试了令牌请求部分,以便提供客户端和用户凭据:
curl -v --data "username=user1&password=user1&client_id=client1&client_secret=client1&grant_type=password" -X POST "http://localhost:8080/samplerestspringoauth2/oauth/token"
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我做了以下观察:
虽然根据我看到的例子,我使用了BasicAuthentication过滤器,但这并没有真正用于安全过程.由于令牌请求不包含Authentication头,因此BasicAuthentication过滤器只会跳过任何检查.ClientCredentialsTokenEndpointFilter和authentication-server是唯一在令牌请求期间执行安全检查的人.注意到并通过调试验证后,我试图完全删除以下部分:
<http-basic entry-point-ref="clientAuthenticationEntryPoint" />
从配置.但后来我收到了警告:
"无法建立AuthenticationEntryPoint.请确保您具有通过命名空间配置的登录机制(例如表单登录)或指定具有'entry-point-ref'属性的自定义AuthenticationEntryPoint".
下一步,我在http命名空间中添加了entry-point-ref ="clientAuthenticationEntryPoint,并取消了警告.我测试了应用程序并正确播放.
但是,除了上述内容之外,我还在调试期间进行了以下观察:ClientCredentialsTokenEndpointFilter在私有变量中包含其自己的OAuth2AuthenticationEntryPoint入口点,并在由于错误的客户端凭据而失败时使用它.因此,无论在基本过滤器中还是在http命名空间中指定的入口点都无关紧要.最后,ClientCredentialsTokenEndpointFilter将使用自己的私有OAuth2AuthenticationEntryPoint.总结一下我的结论似乎如下:
下面我将令牌请求的http和端点配置供您参考.我跳过其余的配置以保持帖子易于阅读:
<http pattern="/oauth/token" create-session="stateless"
authentication-manager-ref="clientAuthenticationManager"
xmlns="http://www.springframework.org/schema/security">
<intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
<anonymous enabled="false" />
<http-basic entry-point-ref="clientAuthenticationEntryPoint" />
<custom-filter ref="clientCredentialsTokenEndpointFilter"
before="BASIC_AUTH_FILTER" />
<access-denied-handler ref="oauthAccessDeniedHandler" />
</http>
<bean id="clientAuthenticationEntryPoint"
class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
<property name="realmName" value="springsec/client" />
<property name="typeName" value="Basic" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我还假设在令牌请求的原始sparklr应用程序(即spring oauth2示例应用程序)配置中也发生了同样的问题,这非常相似.可以在https://github.com/spring-projects/spring-security-oauth/blob/master/samples/oauth2/sparklr/src/main/webapp/WEB-INF/spring-servlet.xml中找到 ,相关部分如下:
<http pattern="/oauth/token" create-session="stateless"
authentication-manager-ref="clientAuthenticationManager"
xmlns="http://www.springframework.org/schema/security">
<intercept-url pattern="/**" method="GET" access="ROLE_DENY" />
<intercept-url pattern="/**" method="PUT" access="ROLE_DENY" />
<intercept-url pattern="/**" method="DELETE" …Run Code Online (Sandbox Code Playgroud) 我有以下返回文件的 spring mvc 方法:
@RequestMapping(value = "/files/{fileName}", method = RequestMethod.GET)
public FileSystemResource getFiles(@PathVariable String fileName){
String path="/home/marios/Desktop/";
return new FileSystemResource(path+fileName);
}
Run Code Online (Sandbox Code Playgroud)
我希望 ResourceHttpMessageConverter 根据其文档使用八位字节流类型创建适当的响应:
如果 JAF 不可用,则使用 application/octet-stream。
但是,尽管我正确地获取文件没有问题,但结果具有Content-Type: application/json;charset=UTF-8
你能告诉我为什么会这样吗?
(我使用 spring 版本 4.1.4。我没有明确设置任何消息转换器,我知道 spring 默认加载 ResourceHttpMessageConverter 和 MappingJackson2HttpMessageConverter,因为我的类路径中有 jackson 2,因为我有其他 mvc返回 json 的方法。
此外,如果我HttpEntity<FileSystemResource>手动使用和设置内容类型,或者用produces = MediaType.APPLICATION_OCTET_STREAM它指定它工作正常。
另请注意,在我的请求中,我没有指定任何接受内容类型,并且不希望依赖我的客户来做到这一点)
我试图在StaticApplicationContext中自动装配一个bean但是虽然我可以插入一个bean并成功检索它,但我无法在另一个bean中自动装配它.下面是一个简单的例子来解释我的意思.
在此示例中,第一个断言成功,第二个断言失败.请注意,如果我注释掉此方法的行,而是取消注释使用AnnotationConfigApplicationContext的方法#2的行,则自动装配将起作用.但是,我想使用StaticApplicationContext方法来完成这项工作.
@Test
public void testAutowire() {
//context configuration approach #1
StaticApplicationContext context = new StaticApplicationContext();
context.registerSingleton("child", Child.class);
//context configuration approach #2
//AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Child.class);
Parent parent = new Parent();
context.getAutowireCapableBeanFactory().autowireBean(parent);
//this is successful
Assert.notNull(context.getBean(Child.class), "no bean found");
//this works only with AnnotationConfigApplicationContext and not with StaticApplicationContext
Assert.notNull(parent.child, "no child autowired");
}
public static class Parent {
@Autowired
Child child;
public Parent() {
}
}
public static class Child {
public Child() {
}
}
Run Code Online (Sandbox Code Playgroud)
问题所在的任何想法?