考虑以下代码:
import java.util.Collections;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.User;
public class SecureStuff {
@PreAuthorize("#user.password == #oldPassword")
public static void changePassword(User user, String oldPassword, String newPassword){
System.out.print("Changing passwords ...");
}
public static void main(String[] args) {
User joe = new User("Joe", "HansWurst", true, true, true, true, Collections.EMPTY_LIST);
changePassword(joe, "HansWurst", "TeeWurst");
}
}
Run Code Online (Sandbox Code Playgroud)
我在STS(SpringSource工具套件)中运行代码,它按预期工作.(它打印出来"Changing passwords ...".)然后我将密码重命名为其他东西,期待方法调用现在失败.
我已将该行添加<global-method-security pre-post-annotations="enabled"/>到applicationContext-security.xml配置文件中.
我在这里错过了什么?
如何在grails中建立两个域类之间的关系.我已经有一个来自插件的域类(spring安全插件),我想将USER域类从插件链接到Profile域类.最好的方法是什么?
任何人都可以通过Spring CAS身份验证(客户端和服务器配置)与我共享示例应用程序.谢谢.
可能重复:
使用Spring Security时,获取bean中当前用户名(即SecurityContext)信息的正确方法是什么?
我正在使用spring security,我想知道如何在我的控制器中检索当前登录的用户名.我可以在某处存储与登录用户名相关联的用户ID吗?目前我从Priciple对象获取用户名
public boolean populateUserName(ModelMap model, Principal principal) {
if (principal != null) {
String name = principal.getName();
model.addAttribute("username", name);
System.out.println("userName - " + name);
return true;
}
else
{
System.out.println("principal is null");
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我在每个控制器方法中获取此Principal对象.我不知道这是否是获得它的最佳方式.例如,这是代码
@RequestMapping(value = "/secure/myitem.htm", method = RequestMethod.GET)
public ModelAndView showItem(@RequestParam("listingId") String listingId,
ModelMap model, Principal principal) {
populateUserName(model, principal);
}
Run Code Online (Sandbox Code Playgroud) 正如标题所暗示的那样,我遇到了一个简单的Spring Security Test的问题.这是我的项目结构(maven webapp 2.5):
main
java
de
cochu
spring
controller
HomeController
webapp
WEB-INF
jsp
home.jsp
index.jsp
security-context.xml
spring-servlet.xml
web.xml
Run Code Online (Sandbox Code Playgroud)
web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/security-context.xml
</param-value>
</context-param>
<filter>
<filter-name>filterChainProxy</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>filterChainProxy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
为spring-servlet.xml
<context:annotation-config/>
<context:component-scan base-package="de.cochu.spring.controller"/>
<bean id="internalViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
安全的context.xml
<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="test" password="test" authorities="ROLE_USER"/>
</security:user-service> …Run Code Online (Sandbox Code Playgroud) 任何人都可以帮我解决如何在自定义登录jsp文件中添加更多字段?默认值为"j_username"和"j_password".如果我想添加更多的领域,即,并希望得到它在我的CustomAuthenticationProvider类扩展AbstractUserDetailsAuthenticationProvider.如果有任何资源或链接,请告诉我.
package hompage
import logindetails.*
import login.User
import grails.plugins.springsecurity.Secured;
class RedirectController
{
def springSecurityService
def index() { }
/**
* This method will redirect the user if it has the role "ROLE_ADMIN"
*/
@Secured(['ROLE_ADMIN'])
def adminhomepage =
{
render(view: "adminhome")
}
/**
* This method will redirect the user if it has the role "ROLE_USER"
*/
@Secured(['ROLE_USER'])
def userhomepage=
{
render(view: "userhome")
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码,我想重定向到adminhome,当且仅当角色是ROLE_ADMIN时,如果角色是ROLE_USER,则重定向到userhome.
请帮我实现这段代码.
我正在使用netbeans IDE,并且有导入导入grails.plugins.springsecurity.Secured的警告(如红色惊叹号,通常显示为java代码)
unable to resolve class grails.plugins.springsecurity.Secured @ line 5, column 1.
Run Code Online (Sandbox Code Playgroud)
即使进口存在,我也没有得到它的问题是什么?
我有一个网站和联系页面.任何人都可以去那里写点东西与我们联系,它将被发送到我的电子邮件地址.今天我注意到收件箱中有近100封电子邮件,有人尝试使用一些javascript发送垃圾邮件.我怎么阻止这个?我想到的一种方法是放一个验证码.但有没有更好的方法来阻止客户端本身而不是检查服务器?
我有以下内容:
def userInstance=User.findAll("from User u where u.merchant is not null and u.merchant.id = ? and u.authorities.authority.contains('ROLE_MERCHANT')", merchantId)
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: ( near line 1, column 121 [from com.testing.tests.User u where u.merchant is not null and u.merchant.id = ? and u.authorities.authority.contains('ROLE_MERCHANT')]
at $Proxy20.createQuery(Unknown Source)
at testing.admin.UserService.findByMerchantId(UserService.groovy:14)
Run Code Online (Sandbox Code Playgroud)
如何仅返回具有特定角色的用户?
谢谢
我的spring安全配置中有多个http块.从Spring Security 3.1版开始就允许这样做.当我的SpringSocial配置尝试自动注入RequestCache对象时,会发生此问题.我收到以下错误:没有定义[org.springframework.security.web.savedrequest.RequestCache]类型的限定bean:期望的单个匹配bean但找到3.如果我将Java代码中的引用删除到自动连接的RequestCache对象,在我的xml配置中有三个http块是可以的.修复此错误的最佳方法是什么?有没有办法自动装配正确的RequestCache?
xml http块:
<http pattern="/oauth/token"...>
</http>
<http pattern="/something/**...>
</http>
<http use-expressions="true"...>
</http>
Run Code Online (Sandbox Code Playgroud)
在Java Config中,有一个自动连接的RequestCache的引用:
@Bean
public ProviderSignInController providerSignInController(RequestCache requestCache) {
return new ProviderSignInController(connectionFactoryLocator(), usersConnectionRepository(), new SimpleSignInAdapter(requestCache, userService));
}
Run Code Online (Sandbox Code Playgroud)
这些都不会导致问题.但是,拥有多个http块和一个自动装配的RequestCache会导致:"没有定义类型[org.springframework.security.web.savedrequest.RequestCache]的限定bean:预期的单个匹配bean但找到3"
任何人都可以帮我一个正确配置的方法吗?
spring-security ×10
spring ×6
grails ×3
groovy ×2
java ×2
spring-mvc ×2
annotations ×1
cas ×1
hibernate ×1
hql ×1
javascript ×1
login-page ×1
security ×1