Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002-2014 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
   * use this file except in compliance with the License. You may obtain a copy of
   * the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and limitations under
  * the License.
  */
 package org.springframework.security.data.repository.query;
 
 
 import java.util.Map;

By defining this object as a Bean, Spring Security is exposed as SpEL expressions for creating Spring Data queries.

With Java based configuration, we can define the bean using the following:

For example, if you return a UserDetails that extends the following User object:

 

Author(s):
Rob Winch
Since:
4.0
Entity:
public class User {
GeneratedValue:
strategy = GenerationType.AUTO)
Id:
private Long id; ...

And you have a Message object that looks like the following:

Entity:
public class Message {
Id:
GeneratedValue:
strategy = GenerationType.AUTO) private Long id;
OneToOne:
private User to; ...
You can use the following Query annotation to search for only messages that are to the current user:
 @Repository
 public interface SecurityMessageRepository extends MessageRepository {
 
  @Query("select m from Message m where m.to.id = ?#{ principal?.id }")
  List<Message> findAll();
 }
 
This works because the principal in this instance is a User which has an id field on it.
 
 
 	}

Creates a new instance that always uses the same org.springframework.security.core.Authentication object.

Parameters:
authentication the org.springframework.security.core.Authentication to use
		this. = authentication;
	}
	public String getExtensionId() {
		return "security";
	}
	public Object getRootObject() {
		Authentication authentication = getAuthentication();
		return new SecurityExpressionRoot(authentication) {
		};
	}
		if ( != null) {
		}
		SecurityContext context = SecurityContextHolder.getContext();
		return context.getAuthentication();
	}
New to GrepCode? Check out our FAQ X