spring-el

Return JSON on unauthorized REST service request using Spring Security 'hasPermission()'

a 夏天 提交于 2019-12-09 18:31:35
问题 I am implementing method level restriction/authorization (on REST services ) using Spring security. I have used the spring expression language and implemented custom Expression Evaluator. It is working fine for me. However, if an unauthorized user tries to access the service, it is responding with a login page. Since my application is REST based only , I want to return only JSON data for all the requests. How do I make it return JSON instead of the login page?(eg: {status : Denied} ) Here is

SpEL in @Qualifier refer to same bean

冷暖自知 提交于 2019-12-09 03:45:08
问题 I am interested to inject a bean reference, which is resolved based on another property on the same bean: @Autowired @Qualifier("#{'prefix' + actualQualifier}") private OtherBean otherBean private String actualQualifier; This would ensure that the relationship between "actualQualifier" and "otherBean" is correct. There is a number of beans configured of the type OtherBean . I can make sure that "actualQualifier" has a value set before autowiring/injection begins. I am unable to find any way

mongodb multi tenacy spel with @Document

元气小坏坏 提交于 2019-12-09 00:09:59
问题 This is related to MongoDB and SpEL Expressions in @Document annotations This is the way I am creating my mongo template @Bean public MongoDbFactory mongoDbFactory() throws UnknownHostException { String dbname = getCustid(); return new SimpleMongoDbFactory(new MongoClient("localhost"), "mydb"); } @Bean MongoTemplate mongoTemplate() throws UnknownHostException { MappingMongoConverter converter = new MappingMongoConverter(mongoDbFactory(), new MongoMappingContext()); return new MongoTemplate

How concatenate two string in Spring Expression Language (SpEL)

孤者浪人 提交于 2019-12-08 21:03:54
问题 In my spring application, the methods from my controller and service classes have this annotation to security purposes: @PreAuthorize("hasPermission(#user, 'cadastra')") the second argument, the permission, should have this format: <<action_name>>_<<class_name>> What expression I should use to accomplish that, taking in consideration the class name is held by this.getClass().getName() ? 回答1: To concatenate two strings in Spring EL you use concat function . See here for more details : Spring

Getting the user home path in application.properties in Spring Boot

。_饼干妹妹 提交于 2019-12-08 16:53:53
问题 This should be a quite straight forward task, but after doing quite a bit of research I'm finding it hard to find any way to do this. I just want to create a log file in the current user's home directory. According to the Official Documentation the variables I should modify are logging.file and logging.path . But how do I get the value of the user-home into the logging.path ? I have tried setting it up like: logging.path=#{systemProperties['user.home']} but without any success. 回答1: If you

Need help in using SPEL in Annotation Value?

房东的猫 提交于 2019-12-08 10:12:10
问题 Tried below SPEL expression, but it fails to work. Need help! @KafkaListener(topics = "#{Arrays.asList(${kafka.topic.helloworld}.split(',')).stream().map(p -> p+envSuffix).toArray(String[]::new)}") 回答1: First of all I see that ${kafka.topic.helloworld} has to be wrapped to the '' , just because property-placeholder works first and then SpEL will treat the result a an active variable. For example you have there, foo,bar,baz . How does it look in Java? Nothing more than wrong code. But when it

SpEL for spring security: Passing Values from XML to Java based SpEL configuration

好久不见. 提交于 2019-12-08 08:53:52
问题 I want to pass property values assigned in an xml file to a Spring expression (an SpEL) in Java. Can you point me out how to achieve that? To make it clear, I've provided the following example. example.xml file: <beans> <bean id="user" class="x.y.User"> <property name="name" value="A"/> <property name="userId" value="33"/> <bean id="customer" class="x.y.Customer"> <property name="name" value="B"/> <property name="customerId" value="33"/> </bean> </beans> Bear in mind that I have 'User' and

Modifying native query cannot have named parameter bindings?

↘锁芯ラ 提交于 2019-12-08 06:30:04
问题 I have specified the following modifying native query within my JpaRepository. public interface PlayerStatisticsRepository extends JpaRepository<PlayerStatistics, PlayerStatisticsKey> { @Modifying @Query(value = "INSERT INTO playerstatistics(enteredTheFieldAtInSeconds, leftTheFieldAtInSeconds, hometeam_name, matchday_matchdaynumber, playedFromTheFirstWhistleblow, player_id) VALUES (:enteredTheFieldAtInSeconds, :leftTheFieldAtInSeconds, :#{#match.primaryKey.homeTeam.name}, :#{#match.primaryKey

How to authenticate against a collection of domain objects in Spring Expression Language SpEL

一笑奈何 提交于 2019-12-08 05:59:34
问题 My principle is an instance of 'foo' that has a collection of 'bars'. Each 'bar' has an id that I want to match with the 'id' passed as the resource requested. Can I do something like the following in Spring SpEL? And if so, how? Example (psuedo syntax; cause I don't know the correct way which is why I am here) @PreAuthorize("principal.transactions.contains(instance where dto.transactionId == instance.id")") public SomeResponse processTransaction(RequestDto dto) { ... } Essentially the

Is it possible to read YAML property into Map using Spring and @Value annotation

浪尽此生 提交于 2019-12-08 00:25:55
问题 What I want to be able to do is: YAML: features: feature1: true feature2: false feature3: true Code: @Value("${features}") private Map<String,Boolean> features; I can't figure out what Spring scripting syntax to use to do this (if it's possible at all) 回答1: I'm using Spring Boot and access custom variables like this: Create a custom class that maps to your custom properties: @Component @ConfigurationProperties(prefix="features") public class ConstantProperties { private String feature1;