Validating user rights instances with spring AOP

Source: Internet
Author: User

1. Create a new Java common project, and import Spring-aop.jar package;

2. Construction of UserInfo class:

package aop.secure;
public class UserInfo {
private String userName;
private String password;
public UserInfo(String userName, String password){
this.userName = userName;
this.password = password;
}
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
}

3. Building security information prompts class Securebean:

package aop.secure;
public class SecureBean {
public void writeSecureMessage(){
System.out.println("Every time I learn something new and it pushes some old stuff out of my brain.");
}
}

4. Securityadvice implementation Org.springframework.aop.MethodBeforeAdvice:

package aop.secure;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class SecurityAdvice implements MethodBeforeAdvice {
private SecurityManager securityManager;
public SecurityAdvice(){
this.securityManager = new SecurityManager();
}
public void before(Method method, Object[] args, Object target) throws Throwable {
UserInfo user = securityManager.getLoggedOnUser();
if(user == null){
System.out.println("No user authenticated.");
throw new SecurityException("Method name: " + method.getName());
}else if("chigo".equals(user.getUserName()) && "chigo".equals(user.getPassword())){
System.out.println("OKAY!");
}else{
System.out.println("Logged in user is: " + user.getUserName());
throw new SecurityException("User " + user.getUserName() + " is not allowed access to method " + method.getName());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.