我正在使用 Spring MVC 公开 RESTful 服务。我已经通过 HTTPBasicAuthentication 启用了身份验证,并使用 <security:http>我可以控制哪些角色可以访问 url。
现在我想使用 @Secured注解。我试图将它添加到 Controller 方法中,但它不起作用。它什么都不做。
这是我的 Controller类:
@Controller
@RequestMapping("/*")
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
private static final String USERS = "/users";
private static final String USER = USERS+"/{userId:.*}";
@RequestMapping(value=USER, method=RequestMethod.GET)
@Secured(value = {"ROLE_ADMIN"})
public @ResponseBody User signin(@PathVariable String userId) {
logger.info("GET users/"+userId+" received");
User user= service.getUser(userId);
if(user==null)
throw new ResourceNotFoundException();
return user;
}
}
这是我的 security-context.xml :
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER"/>
</http>
<global-method-security secured-annotations="enabled" />
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin@somedomain.com" password="admin"
authorities="ROLE_USER, ROLE_ADMIN" />
<user name="user@somedomain.com" password="pswd"
authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
还有我的root-context.xml :
<context:component-scan base-package="org.mypackage" />
<import resource="database/DataSource.xml"/>
<import resource="database/Hibernate.xml"/>
<import resource="beans-context.xml"/>
<import resource="security-context.xml"/>
一切正常,但如果我添加 @Secured ,它什么都不做:我也可以使用 user@somedomain.com 访问安全方法,它没有 ROLE_ADMIN 权限。
我已经尝试移动 <security:global-method-security>至 root-context.xml , 它不起作用。我还尝试通过 <security:http> 保护相同的方法标签,它工作正常,但我想使用 @Secured注释。
谢谢。
编辑:
我还有一个 servlet-context.xml和一个 controllers.xml appServlet 子目录中的配置文件。
这里是 servlet-context.xml :
<mvc:resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:import resource="controllers.xml" />
和controllers.xml :
<context:component-scan base-package="org.mose.emergencyalert.controllers" />
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<beans:bean id="homeController" class="org.mose.emergencyalert.controllers.HomeController"/>
最佳答案
解决了,我加了<global-method-security>在 servlet-context.xml 中标记, 而不是 security-context.xml .
这是新的 security-context.xml :
<annotation-driven />
<security:global-method-security secured-annotations="enabled"/>
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
注意:现在 Eclipse 在 <security:global-method-security> 处警告我行:“advises org.mypackage.HomeController.signin(String,
Principal) ”,证明 @Secured现在正在工作。
关于java - 无法让 @Secured 在 Spring MVC 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14436785/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'