UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
<properties> <spring.version>3.1.4.RELEASE</spring.version> </properties> <!-- Spring Security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring.version}</version> </dependency>
<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>
<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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> <http access-denied-page="/error403.jsp"> <intercept-url pattern="/index*" access="ROLE_USER,ROLE_ANONYMOUS"/> <intercept-url pattern="/add*" access="ROLE_USER"/> <intercept-url pattern="/delete/*" access="ROLE_ADMIN"/> <form-login login-page="/login.jsp" default-target-url="/index" authentication-failure-url="/login.jsp?error=true"/> <logout logout-url="/logout" logout-success-url="/index"/> <anonymous username="guest" granted-authority="ROLE_ANONYMOUS"/> <remember-me/> </http> <authentication-manager> <authentication-provider> <user-service> <user name="admin" password="pass" authorities="ROLE_ADMIN,ROLE_USER"/> <user name="user1" password="1111" authorities="ROLE_USER"/> <user name="user2" password="2222" disabled="true" authorities="ROLE_USER"/> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
<global-method-security secured-annotations="enabled" />
public interface AdminService { @Secured("ROLE_ADMIN") public Account editAccount(Account account); }
<beans xmlns="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-2.5.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/<yourDataBaseName>" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> <authentication-provider> <jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username, password, enabled from users where username = ?" authorities-by-username-query="select u.username, au.authority from users u, authorities au where u.id = au.user_id and u.username = ?" /> </authentication-provider> </beans>
<authentication-manager> <authentication-provider> <password-encoder hash="sha"/> <user-service> <user name="jimi" password="d7e6351eaa13189a5a3641bab846c8e8c69ba39f" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="bob" password="4e7421b1b8765d8f9406d87e7cc6aa784c4ab97f" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> <password-encoder hash="sha"> <salt-source user-property="username"/> </password-encoder>
Source: https://habr.com/ru/post/203318/
All Articles