gpt4 book ai didi

Apache Shiro 用于保护 REST api

转载 作者:行者123 更新时间:2023-12-03 18:05:37 26 4
gpt4 key购买 nike

我正在尝试将 Shiro 集成到我的 spring mvc 应用程序中。
身份验证由 LDAP 服务器支持,我能够成功通过 ldap 服务器进行身份验证并获取 cookie。

我无法执行的是在后续请求中使用此 cookie 并获得结果。尝试使用 cookie 会给我一个 HTTP 302 来再次执行身份验证。例如:
获取 ( rest/assets/list ) 标题 Cookie: JSESSIONID=abcd重定向到 ( rest/login )

这是保护 api 的正确策略吗?
AngularJS 应用程序正在使用该 api,我想在添加 CRUD 功能之前启用基于用户组的身份验证。

任何指针都会有用。

源代码如下:

applicationContext.xml 文件如下

 <beans xmlns="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.xsd">


<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"/>

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>

<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>

<bean id="ldapRealm" class="com.directv.nmsupport.security.LDAPRealm">
<property name="contextFactory" ref="ldapContextFactory" />
<property name="userDnTemplate" value="uid={0},ou=DirecTV,ou=People,dc=swengdtv,dc=net" />
</bean>

<bean id="ldapContextFactory" class="org.apache.shiro.realm.ldap.JndiLdapContextFactory">
<property name="url" value="ldap://teon:389"/>
</bean>

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="ldapRealm"/>
<property name="cacheManager" ref="cacheManager"/>
<property name="sessionManager" ref="sessionManager" />
</bean>

<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionIdCookieEnabled" value="true" />
</bean>

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/rest/login"/>
<property name="filterChainDefinitions">
<value>
/rest/login = anon
/rest/** = user
</value>
</property>
</bean>
</beans>

LDAPRealm.java 代码是

public class LDAPRealm extends JndiLdapRealm {

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = (String) getAvailablePrincipal(principals);
return super.doGetAuthorizationInfo(principals); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
return super.doGetAuthenticationInfo(token); //To change body of overridden methods use File | Settings | File Templates.
}

}

LoginController.java 是

@Controller
public class LoginController {

@RequestMapping(value = "/login", method = RequestMethod.POST)
public void login(@RequestBody UserCredentials user) {
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
token.setRememberMe(true);
SecurityUtils.getSecurityManager().authenticate(token);
Subject subject = SecurityUtils.getSubject();
subject.getSession(true);
}

@RequestMapping(value="/logout")
public void logout() {
Subject subject = SecurityUtils.getSubject();
SecurityUtils.getSecurityManager().logout(subject);
}

}

来自 apache shiro 的日志记录是
17:47:54.428 ["http-bio-8080"-exec-7] DEBUG o.a.shiro.realm.ldap.JndiLdapRealm - Authenticating user 'afulara' through LDAP
17:47:54.428 ["http-bio-8080"-exec-7] DEBUG o.a.s.r.ldap.JndiLdapContextFactory - Initializing LDAP context using URL [ldap://teon:389] and principal [uid=afulara,ou=DirecTV,ou=People,dc=swengdtv,dc=net] with pooling disabled
17:47:54.431 ["http-bio-8080"-exec-7] DEBUG o.a.shiro.realm.AuthenticatingRealm - Looked up AuthenticationInfo [afulara] from doGetAuthenticationInfo
17:47:54.431 ["http-bio-8080"-exec-7] DEBUG o.a.shiro.realm.AuthenticatingRealm - AuthenticationInfo caching is disabled for info [afulara]. Submitted token: [org.apache.shiro.authc.UsernamePasswordToken - afulara, rememberMe=true].
17:47:54.431 ["http-bio-8080"-exec-7] DEBUG o.a.s.authc.AbstractAuthenticator - Authentication successful for token [org.apache.shiro.authc.UsernamePasswordToken - afulara, rememberMe=true]. Returned account [afulara]
17:48:20.927 ["http-bio-8080"-exec-9] DEBUG o.a.shiro.web.servlet.SimpleCookie - Found 'JSESSIONID' cookie value [02b41ee8-e9e3-43e5-8ee3-aae72322fede]
17:48:24.204 ["http-bio-8080"-exec-10] DEBUG o.a.shiro.web.servlet.SimpleCookie - Found 'JSESSIONID' cookie value [02b41ee8-e9e3-43e5-8ee3-aae72322fede]
17:48:24.210 ["http-bio-8080"-exec-10] WARN o.s.web.servlet.PageNotFound - Request method 'GET' not supported

最佳答案

REST 背后的理念是它是无状态的,因此,如果您与客户端创建 session ,您将破坏 RESTful API 的原则。我建议使用 Spring Security,你可以在这里找到一个简单的例子:http://spring.io/guides/gs/securing-web/ .大约需要 5 - 10 分钟,然后您就可以在您的项目中使用它。

还有一个连接到 LDAP 系统的简单指南:
http://spring.io/guides/gs/authenticating-ldap/

更新:看到你对你的问题的评论,我建议你用你想出的结果来回答你的问题。

关于Apache Shiro 用于保护 REST api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16804032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com