我正在使用Spring Security来保护Struts2 Web应用程序.由于项目限制,我使用的是Spring Security 2.06.
我的团队构建了一个自定义用户管理API,用于在接收用户名和密码参数后对用户进行身份验证,并返回包含角色列表和其他属性(如电子邮件,名称等)的自定义用户对象.
根据我的理解,典型的Spring Security用例使用默认的UserDetailsService来检索UserDetails对象; 此对象将包含(以及其他内容)框架将用于对用户进行身份验证的密码字段.
在我的情况下,我想让我们的自定义API执行身份验证,然后返回包含角色和其他属性(电子邮件等)的自定义UserDetails对象.
经过一些研究,我发现我可以通过AuthenticationProvider的自定义实现来实现这一点.我也有UserDetailsService和UserDetails的自定义实现.
我的问题是我真的不明白我应该在CustomAuthenticationProvider中返回什么.我在这里使用自定义UserDetailsService对象吗?甚至需要吗?对不起,我真的很困惑.
CustomAuthenticationProvider:
public class CustomAuthenticationProvider implements AuthenticationProvider {
private Logger logger = Logger.getLogger(CustomAuthenticationProvider.class);
private UserDetailsService userDetailsService; //what am i supposed to do with this?
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) authentication;
String username = String.valueOf(auth.getPrincipal());
String password = String.valueOf(auth.getCredentials());
logger.info("username:" + username);
logger.info("password:" + password);
/* what should happen here? */
return null; //what do i return?
}
@Override
public boolean supports(Class aClass) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用mvc-test测试我的登录页面.在我添加spring security之前,我工作得很好.
我的代码是:
mockMvc.perform(
post("j_spring_security_check")
.param(LOGIN_FORM_USERNAME_FIELD, testUsernameValue)
.param(LOGIN_FORM_PASSWORD_FIELD, testPasswordValue))
.andDo(print())
.andExpect(status().isOk())
.andExpect(model().attribute(LOGIN_PAGE_STATUS_VALUE, LOGIN_PAGE_STATUS_FALSE_INDICATOR));
Run Code Online (Sandbox Code Playgroud)
测试类添加了正确的注释:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:security-context.xml", "classpath:applicationContext.xml", "classpath:test-contexts/test-context.xml" })
Run Code Online (Sandbox Code Playgroud)
我的过滤器已定义(在web.xml中):
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)
当我尝试在@ContextConfiguration中添加web.xml时,它会失败,当我删除它时,我得到一个例外:
java.lang.AssertionError: Status expected:<200> but was:<405>
Run Code Online (Sandbox Code Playgroud)
有没有办法添加DelegatingProxyFilter来测试上下文与我的security-context.xml中定义的配置,使其工作?我尝试了一些注入FilterProxyChain的教程,但它不适用于我的情况.
有人可以帮助我吗?提前致谢
我前段时间编写了一些代码,用于从十六进制编码的字符串文字OverloadedStrings
创建ByteString
s,它使用提供的函数进行解码base16-bytestring
.这工作得很好,但似乎我并没有像我想的那样理解它.
让我完全困惑的是这个.为什么
{-# LANGUAGE OverloadedStrings #-}
import Data.ByteString.Base16 ()
import qualified Data.ByteString as B
plaintext = "The message" :: B.ByteString
main = print plaintext
Run Code Online (Sandbox Code Playgroud)
编译并运行OK,但如果我删除导入Data.ByteString.Base16
然后它无法编译(类似于这个问题):
test.hs:6:13:
No instance for (Data.String.IsString B.ByteString)
arising from the literal `"The message"'
Run Code Online (Sandbox Code Playgroud)
根据Haskell Wiki,这样的导入"仅对导入类型类的实例很有用",但据我所知,base16-bytestring源代码没有定义任何类型类实例,只是encode
和decode
函数.
导入如何IsString
为代码编译提供必要的实例?
仅当用户通过身份验证时,我才在JSP页面中显示注销链接时遇到问题.以下是我在JSP页面这一行的例外情况:
<sec:authorize access="isAuthenticated()">
Run Code Online (Sandbox Code Playgroud)
例外:
Stacktrace:
....
root cause
javax.servlet.jsp.JspException: No visible WebSecurityExpressionHandler instance could be found in the application context. There must be at least one in order to support expressions in JSP 'authorize' tags.
org.springframework.security.taglibs.authz.AuthorizeTag.getExpressionHandler(AuthorizeTag.java:100)
org.springframework.security.taglibs.authz.AuthorizeTag.authorizeUsingAccessExpression(AuthorizeTag.java:58)
Run Code Online (Sandbox Code Playgroud)
这是我的application-context-Security.xml:
<http auto-config='true' >
<intercept-url pattern="/user/**" access="ROLE_User" />
<logout logout-success-url="/hello.htm" />
</http>
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService" />
</beans:bean>
<beans:bean id="authenticationManager"
class="org.springframework.security.authentication.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider" />
</beans:list>
</beans:property>
</beans:bean>
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder hash="plaintext" />
</authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)
我知道我可以在http标签中使用use-expression ="true",但这意味着我必须在intercept-url标签和java代码中使用表达式.有解决方法吗?
我需要一个包的次要版本,例如persistent-postgresql ..或hdbc-mysql或者其他什么.
如果我跑
cabal install persistent-postgresql
Run Code Online (Sandbox Code Playgroud)
它将安装persistent-postgresql版本1.3然后我得到错误'至少缺少以下依赖项:persistent-postgresql> = 1.2 && <1.3'这就是为什么我想安装此软件包的早期版本以使其工作.或者也许有另一种解决方案?
提前致谢.
在我们的应用程序中,我们计划使用该RoleVoter
机制但我们想删除ROLE_
前缀,因为我们正在实现的安全性比基于角色的任务更基于任务.
从技术上讲,实现没有问题,但我在文档中发现,不鼓励使用RoleVoter
带有空前缀.
我想知道为什么?
AFAICS,唯一的问题是,如果没有前缀时,RoleVoter
将参与决策,这并不意味着(如IS_AUTHENTICATED_FULLY
,IS_AUTHENTICATED_REMEMBERED
...)和威力返回否认,而不是弃权的访问.
您能否确认这是唯一一个空前缀的问题?
在此先感谢M.
当我version
在sbt控制台中运行命令时,它显示"0.1.0-SNAPSHOT".这是目前的sbt版本吗?
我使用SBT wiki上的入门指南在我的系统上安装sbt.
我想记录我的Web应用程序中的每个登录.我能够访问通过登录进行的登录,UsernamePasswordAuthenticationFilter
但我不知道如何登录使用remember-me功能登录的用户.我试过压倒了
createSuccessfulAuthentication(HttpServletRequest request, UserDetails user)
Run Code Online (Sandbox Code Playgroud)
的TokenBasedRememberMeServices
,但随后注销记录太多,因为记得我服务的重新认证用户.
我正在使用svn log --xml
响应来查看特定修订的更改.但是如果版本或路径无效,我只想得到一个空白但有效的xml响应.有可能吗?
所以不是这样的:
<?xml version="1.0"?>
<log>
svn: Unable to find repository location for 'http://subversion.ny.jpmorgan.com /svn/repos/IM_RPS_CORE/rps_deploy_tools_content_test/branches/rol-201106-content-test' in revision 1556
Run Code Online (Sandbox Code Playgroud)
我可以得到:
<?xml version="1.0"?>
<log>
</log>
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有一些代码看起来像这样(大大简化的版本)。函数接受两个类型为LoadClient
and的函数参数CheckApproval
,并返回错误或字符串。
pub struct Client {
pub id: String,
}
pub enum MyErr {
RequiresApproval(Client, String),
LoadFailed,
}
pub fn authorize<LoadClient, CheckApproval>(load_client: LoadClient, check_approval: CheckApproval) -> Result<String, MyErr>
where
LoadClient: FnOnce(String) -> Result<Client, String>,
CheckApproval: for<'a> FnOnce(&'a Client, &str) -> Result<&'a str, ()>,
{
let client = load_client("hello".to_string()).map_err(|_| MyErr::LoadFailed)?;
let permission = "something";
// This doesn't compile
// let authorized = check_approval(&client, permission).map_err(|_| MyErr::RequiresApproval(client, permission.to_string()))?;
// Ok(authorized.to_string())
// This version does
match check_approval(&client, permission) {
Err(_) => …
Run Code Online (Sandbox Code Playgroud)