看起来我的@Controller中的方法看起来不像@Secured.当使用基于sec:intercept-url的安全过滤时,这似乎工作正常.以下代码导致Spring Security向我提供此日志条目:
DEBUG:org.springframework.security.web.access.intercept.FilterSecurityInterceptor - 公共对象 - 未尝试身份验证
web.xml中
contextConfigLocation /WEB-INF/spring/root-context.xml
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Filter security -->
<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)
servlet-context.xml包含viewResolvers和所有编组的配置.此配置是注释驱动的.
根的context.xml
<sec:global-method-security secured-annotations="enabled" />
<sec:http auto-config="true">
<sec:http-basic/>
</sec:http>
<!-- Declare an authentication-manager to use a custom userDetailsService -->
<sec:authentication-manager>
<sec:authentication-provider
user-service-ref="userDetailsService">
<sec:password-encoder ref="passwordEncoder" />
</sec:authentication-provider>
</sec:authentication-manager> …Run Code Online (Sandbox Code Playgroud) 在发布此问题之前,我谷歌从Spring项目获取属性(它不是基于Web的项目).我很困惑,因为每个人都在谈论application-context.xml并且配置类似
但是,我正在使用Spring正常的Java项目(没有Web应用程序和类似的东西).但我想从属性文件中获取一些常见属性,并且需要在JAVA文件中使用.如何通过使用Spring/Spring Annotations实现这一目标?
我应该在我的项目下配置myprops.properties文件以及如何通过spring调用?
我的理解是application-context.xml仅用于基于Web的项目.如果没有,我应该如何配置这个application-context.xml,因为我没有web.xml来定义application-context.xml
我有一个直接提供fooBean的java配置类和通过组件扫描提供barBean.
@Configuration
@ComponentScan(basePackages = { "com.blah" })
public class Config {
@Bean
public FooBean fooBean {
return new FooBean();
}
}
Run Code Online (Sandbox Code Playgroud)
我想在测试用例中重用它,我需要用模拟替换bean:
@Configuration
@Import(Config.class)
public class TestConfig {
@Bean
public FooBean fooBean {
return new FooBeanMock();
}
@Bean
public BarBean barBean {
return new BarBeanMock();
}
}
Run Code Online (Sandbox Code Playgroud)
(这里重用Config没有多大意义,但在现实生活中我有1000豆,我只需要模拟一些)
这里fooBean被覆盖,但不是barBean.
Skipping loading bean definition for %s: a definition for bean " + "'%s' already exists. This is likely due to an override in XML.
Run Code Online (Sandbox Code Playgroud)
还有一个官方问题:https: //jira.springsource.org/browse/SPR-9682
有人知道覆盖组件扫描发现的bean的任何解决方法吗?
考虑到bean是遗留代码并且无法修改,并且其依赖项没有setter(私有属性+ @Resource).
我正在使用Spring Boot注释配置.我有一个类,其构造函数接受2个参数(字符串,另一个类).
Fruit.java
public class Fruit {
public Fruit(String FruitType, Apple apple) {
this.FruitType = FruitType;
this.apple = apple;
}
}
Run Code Online (Sandbox Code Playgroud)
Apple.java
public class Apple {
}
Run Code Online (Sandbox Code Playgroud)
我有一个类需要通过向构造函数注入参数来自动装配上面的类("iron Fruit",Apple类)
Cook.java
public class Cook {
@Autowired
Fruit applefruit;
}
Run Code Online (Sandbox Code Playgroud)
厨师课需要使用参数自动装配Fruit类("铁水果",Apple类)
XML配置如下所示:
<bean id="redapple" class="Apple" />
<bean id="greenapple" class="Apple" />
<bean name="appleCook" class="Cook">
<constructor-arg index="0" value="iron Fruit"/>
<constructor-arg index="1" ref="redapple"/>
</bean>
<bean name="appleCook2" class="Cook">
<constructor-arg index="0" value="iron Fruit"/>
<constructor-arg index="1" ref="greenapple"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
如何仅使用注释配置来实现它?
目前我正在学习Spring框架,主要关注它的安全模块.我已经看过一些与注册和登录相关的指南.我在User类的密码字段中看到了transient关键字或@Transient注释的常见用法.
我的虚拟应用程序使用Spring Boot + Spring MVC + Spring Security + MySQL.
我知道
Java的transient关键字用于表示字段不是序列化的.
JPA的@Transient注释 ......
...指定属性或字段不是持久的.它用于注释实体类,映射的超类或可嵌入类的属性或字段.
和org.springframework.data.annotation的@Transient批注...
将字段标记为映射框架的瞬态字段.因此,该属性将不会被持久化,也不会被映射框架进一步检查.
在我的MySQL数据库中,我有spring_demo模式,它有3个表:
+-----------------------+
| Tables_in_spring_demo |
+-----------------------+
| role |
| user |
| user_role |
+-----------------------+
Run Code Online (Sandbox Code Playgroud)
当我在User类的密码字段中使用transient关键字时,它不会存储在MySQL数据库中.(例如:test01)
mysql> select * from user;
+----+--------+------------------+----------+
| id | active | email | username |
+----+--------+------------------+----------+
| 1 | 1 | test01@gmail.com | test01 |
+----+--------+------------------+----------+ …Run Code Online (Sandbox Code Playgroud) jpa spring-security transient spring-annotations spring-data
当我注意到我的客户端应用程序(基于Swing)上使用疯狂的高RAM时,我开始研究它,看起来这与Spring中基于Annotation的配置有某种关系.正如您将在下面的编辑中看到的那样,我意识到这只发生在64位JVM上.
请参阅以下测试代码:
基于xml的配置
<beans ....>
<bean id="xmlConfigTest" class="at.test.XmlConfigTest" />
</beans>
public class XmlConfigTest extends JFrame {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("config/applicationContext.xml");
XmlConfigTest frame = (XmlConfigTest) ctx.getBean("xmlConfigTest");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
使用大约32MB内存,这对我来说似乎没问题.
现在与基于注释的配置相同:
@Service
public class AnnotationConfigTestFrame extends JFrame {
public static void main(String[] args) throws InterruptedException {
ApplicationContext ctx = new AnnotationConfigApplicationContext("at.test");
AnnotationConfigTestFrame frame = (AnnotationConfigTestFrame) ctx
.getBean("annotationConfigTestFrame");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
打开相框不仅要花费更长的时间,而且在启动时内存消耗量会升至160MB内存,然后在大约152MB处达到平衡,这对我来说似乎非常高.请记住,这只是最基本的情况,我开发的客户端应用程序已经超过400MB,这对于旧机器来说太多了.
有没有人对这种行为有解释?我不明白..
(在这里使用3.1.1.RELEASE顺便说一下.)
编辑* 正如axtavt所建议的,我还尝试直接用Test-Class作为Argument构造AnnotationConfigApplicationContext,这样就不需要类路径扫描.遗憾的是,没有改变内存消耗.
编辑2已删除,请参阅编辑3
编辑3 我现在使用32位和64位JVM以及上面的测试程序在同一台机器(Windows 7 …
我已经开始开发一个新的Spring 3.2.4应用程序,我正在尝试使用基于Java的配置而不是过去使用过的XML文件.但是,我无法进行转换.
使用XML,我会按如下方式编写代码:
<!-- application datasource -->
<bean id="dataSource.jndi" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton" lazy-init="true">
<property name="jndiName" value="java:comp/env/jdbc/liment" />
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource.jndi"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
但是,我非常想在Java中弄清楚如何做到这一点.我正在尝试复制配置,但遇到了麻烦:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages={"com.ia"})
public class AppConfigJPA {
@Bean
public DataSource dataSource() {
// configure and return the necessary JDBC DataSource
JndiObjectFactoryBean dataSource = new JndiObjectFactoryBean();
dataSource.setJndiName("java:comp/env/jdbc/liment");
try {
dataSource.afterPropertiesSet();
} catch (IllegalArgumentException | NamingException e) {
// rethrow
throw new RuntimeException(e);
} …Run Code Online (Sandbox Code Playgroud) 我在tomcat webserver环境中运行war文件.
我有一个基于注释的配置@Beans,以及一个用于webservices的xml配置:
@Configuration
//@ComponentScan(basePackageClasses = ...)
public class AppConfig {
//beans @Bean
}
Run Code Online (Sandbox Code Playgroud)
applicationContext.xml中:
<beans>
<context:component-scan base-package="..."/>
<jaxws:endpoint ... />
</bean>
Run Code Online (Sandbox Code Playgroud)
问题:我想@ComponentScan通过注释定义只有类型安全.但如果我这样做,则不会执行扫描.相比之下,当我使用<context:component-scan..一切正常.
SpringWeb服务器中的组件扫描是否与用于包扫描的xml配置相关联?
我使用@ScheduledSpring框架中的注释来调用方法.但是我的设置中有多个节点,我不希望它们全部在同一时间运行.所以我想将一个随机值设置为初始延迟,以使它们相互抵消.
import org.springframework.scheduling.annotation.Scheduled;
@Scheduled(fixedRate = 600000, initialDelay = <random number between 0 and 10 minutes> )
Run Code Online (Sandbox Code Playgroud)
不幸的是,我只允许在这里使用常量表达式.还有其他方法吗?我想过使用Spring表达式语言.
在我的控制器中,我有String参数,包含一些id,不应该是空字符串的null.我想知道,有没有办法检查它是不是@RequestMapping params中的空字符串?我试图在某些方面解决它
@RequestMapping(value = someURL, params = {"id"})
public SomeResponse doSomething(@RequestParam(required = true) String id)
@RequestMapping(value = someURL, params = {"!id="})
public SomeResponse doSomething(@RequestParam(required = true) String id)
@RequestMapping(value = someURL, params = {"!id=\"\""})
public SomeResponse doSomething(@RequestParam(required = true) String id)
Run Code Online (Sandbox Code Playgroud)
没有成功.据我了解,双方params = {"id"}并@RequestParam(required = true)只能检查参数id呈现在请求(!= NULL).
我很可能必须使用控制器boby中的代码检查它,例如
if (id == null || id.isEmpty()) {
return someErrorResponse;
}
Run Code Online (Sandbox Code Playgroud)
但如果我错了,请纠正我.提前致谢.
PS我的应用程序在Apache Tomcat 7.0.62容器中的Java 1.7 SE上运行
java spring spring-mvc hibernate-validator spring-annotations
spring ×8
java ×5
spring-mvc ×2
jpa ×1
jvm ×1
properties ×1
spring-3 ×1
spring-boot ×1
spring-data ×1
transient ×1