package com.yiibai.springsecurity.configuration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("yiibai").password("123456").roles("USER"); auth.inMemoryAuthentication().withUser("admin").password("123456").roles("ADMIN"); auth.inMemoryAuthentication().withUser("dba").password("123456").roles("ADMIN","DBA"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/", "/home").access("hasRole('USER') or hasRole('ADMIN') or hasRole('DBA')") .and().formLogin().loginPage("/login") .usernameParameter("ssoId").passwordParameter("password") .and().exceptionHandling().accessDeniedPage("/Access_Denied"); } }
@EnableGlobalMethodSecurity 啟用 Spring Security 全域性方法可以使用如下XML組態:
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd"> <http auto-config="true" > <intercept-url pattern="/" access="hasRole('USER') or hasRole('ADMIN') and hasRole('DBA')" /> <intercept-url pattern="/home" access="hasRole('USER') or hasRole('ADMIN') and hasRole('DBA')" /> <form-login login-page="/login" username-parameter="ssoId" password-parameter="password" authentication-failure-url="/Access_Denied" /> </http> <global-method-security pre-post-annotations="enabled"/> <authentication-manager > <authentication-provider> <user-service> <user name="bill" password="abc123" authorities="ROLE_USER" /> <user name="admin" password="root123" authorities="ROLE_ADMIN" /> <user name="dba" password="root123" authorities="ROLE_ADMIN,ROLE_DBA" /> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
@Secured注釋是用來定義業務方法的安全性組態屬性列表。您可以使用@Secured在方法上指定安全性要求[角色/許可權等],只有對應角色/許可權的使用者才可以呼叫這些方法。如果有人試圖呼叫一個方法,但是不擁有所需的角色/許可權,那會將會拒絕存取將引發異常。
package com.yiibai.springsecurity.service; import org.springframework.security.access.annotation.Secured; public interface UserService { List<User> findAllUsers(); @Secured("ROLE_ADMIN") void updateUser(User user); @Secured({ "ROLE_DBA", "ROLE_ADMIN" }) void deleteUser(); }
package com.yiibai.springsecurity.service; import org.springframework.security.access.prepost.PostAuthorize; import org.springframework.security.access.prepost.PreAuthorize; import com.yiibai.springsecurity.model.User; public interface UserService { List<User> findAllUsers(); @PostAuthorize ("returnObject.type == authentication.name") User findById(int id); @PreAuthorize("hasRole('ADMIN')") void updateUser(User user); @PreAuthorize("hasRole('ADMIN') AND hasRole('DBA')") void deleteUser(int id); }
package com.yiibai.springsecurity.service; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.yiibai.springsecurity.model.User; @Service("userService") @Transactional public class UserServiceImpl implements UserService{ static List<User> users = new ArrayList<User>(); static{ users = populateUser(); } public List<User> findAllUsers(){ return users; } public User findById(int id){ for(User u : users){ if(u.getId()==id){ return u; } } return null; } public void updateUser(User user) { System.out.println("Only an Admin can Update a User"); User u = findById(user.getId()); users.remove(u); u.setFirstName(user.getFirstName()); u.setLastName(user.getLastName()); u.setType(user.getType()); users.add(u); } public void deleteUser(int id){ User u = findById(id); users.remove(u); } private static List<User> populateUser(){ List<User> users = new ArrayList<User>(); users.add(new User(1,"Sam","Disilva","admin")); users.add(new User(2,"Kevin","Brayn","admin")); users.add(new User(3,"Nina","Conor","dba")); users.add(new User(4,"Tito","Menz","dba")); return users; } }
public class User { private int id; private String firstName; private String lastName; private String type; //getters/setters }
package com.yiibai.springsecurity.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.yiibai.springsecurity.model.User; import com.yiibai.springsecurity.service.UserService; @Controller public class HelloWorldController { @Autowired UserService service; @RequestMapping(value = { "/", "/list" }, method = RequestMethod.GET) public String listAllUsers(ModelMap model) { List<User> users = service.findAllUsers(); model.addAttribute("users", users); return "allusers"; } @RequestMapping(value = { "/edit-user-{id}" }, method = RequestMethod.GET) public String editUser(@PathVariable int id, ModelMap model) { User user = service.findById(id); model.addAttribute("user", user); model.addAttribute("edit", true); return "registration"; } @RequestMapping(value = { "/edit-user-{id}" }, method = RequestMethod.POST) public String updateUser(User user, ModelMap model, @PathVariable int id) { service.updateUser(user); model.addAttribute("success", "User " + user.getFirstName() + " updated successfully"); return "success"; } @RequestMapping(value = { "/delete-user-{id}" }, method = RequestMethod.GET) public String deleteUser(@PathVariable int id) { service.deleteUser(id); return "redirect:/list"; } @RequestMapping(value = "/Access_Denied", method = RequestMethod.GET) public String accessDeniedPage(ModelMap model) { model.addAttribute("user", getPrincipal()); return "accessDenied"; } @RequestMapping(value = "/login", method = RequestMethod.GET) public String loginPage() { return "login"; } @RequestMapping(value="/logout", method = RequestMethod.GET) public String logoutPage (HttpServletRequest request, HttpServletResponse response) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth != null){ new SecurityContextLogoutHandler().logout(request, response, auth); } return "redirect:/login?logout"; } private String getPrincipal(){ String userName = null; Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof UserDetails) { userName = ((UserDetails)principal).getUsername(); } else { userName = principal.toString(); } return userName; } }
開啟瀏覽器,並開啟網址:http://localhost:8080/SpringSecurityMethodLevelSecurityAnnotation/, 系統將提示您登入。
就這麼多,包教不包會。