我正在尝试设置一个使用的 Web 应用程序
我开始使用这个例子SpringMVC-Atmosphere-Example。
使用这个 web.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" id="WebApp_ID" metadata-complete="true" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Example Project</display-name>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>gui-dispatcher</servlet-name>
<servlet-class>org.atmosphere.cpr.MeteorServlet</servlet-class>
<async-supported>true</async-supported>
<init-param>
<param-name>org.atmosphere.servlet</param-name>
<param-value>org.springframework.web.servlet.DispatcherServlet</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.broadcasterClass</param-name>
<param-value>org.atmosphere.cpr.DefaultBroadcaster</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.broadcaster.shareableThreadPool</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.useNative</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.useWebSocket</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.useStream</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>gui-dispatcher</servlet-name>
<url-pattern>/main/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/security/spring-security.xml
/WEB-INF/spring-context.xml …Run Code Online (Sandbox Code Playgroud) 每个实体类都有 user.id 值,我在所有服务上都有过滤器,它们在数据库级别按 principal.id 和实体 user.id 过滤数据,只需添加 where 子句。我开始使用@Cacheable spring 选项。但是过滤器不适用于 spring-cache。如何从缓存中过滤数据?
@Override
@Cacheable(value = "countries")
public List<Country> getAll() {
return countryDao.findAll();
}
Run Code Online (Sandbox Code Playgroud)
如果值在缓存中,不同的用户可以访问其他用户的值。
我按照本指南https://spring.io/guides/gs/securing-web/guide为我的微服务设置 spring 安全
我只是发送基本的 POST 和 GET 请求。我可以执行 GET 请求,但是当我尝试 POST 请求时,出现 403 错误。(“未找到预期的 CSRF 令牌。您的会话是否已过期?”)我正在尝试为我的微服务设置基本身份验证
谢谢
我的应用程序可以有以下 URL:
/siteadmin/homepage/
/siteusers/customer/createCustomer
Run Code Online (Sandbox Code Playgroud)
以下是我的spring-security.xml:
/siteadmin/homepage/
/siteusers/customer/createCustomer
Run Code Online (Sandbox Code Playgroud)
如果我使用用户“a”登录并点击 URL http://localhost:8080/siteadmin/homepage/,则允许用户“a”查看页面,尽管他的角色不是admin。但是当我尝试点击时,http://localhost:8080/siteadminSpring Security 工作正常,即。它显示访问被拒绝页面。我想限制/admin/*没有Admin角色的用户的 URL 。
这是我使用 Spring Boot 和 Spring Security 的代码。问题是当我曾经注销(使用Thyemleaf)时,注销对我不起作用。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username as principal, password as credentials,active from users where username=?")
.authoritiesByUsernameQuery("select username as principal,roles as role from users_roles where username=?")
.rolePrefix("ROLE_")
.passwordEncoder(new Md5PasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage("/login");
http
.authorizeRequests()
.antMatchers("/index1").permitAll();
http
.authorizeRequests()
.antMatchers("/user").hasRole("USER")
.and()
.logout();
http
.authorizeRequests()
.antMatchers("/adpage").hasRole("ADMIN");
http
.exceptionHandling().accessDeniedPage("/403"); …Run Code Online (Sandbox Code Playgroud) 我发现了许多类似的问题,但是都没有解决我的问题,我的问题是:PreAuthorize(“ isAuthenticated()”)在我的RestController上不起作用。
我的配置安全性是:
<global-method-security pre-post-annotations="enabled"/>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<password-encoder ref="passwordEncoder" />
<jdbc-user-service
data-source-ref="dataSource"
users-by-username-query="
select login,password,1
from test tst where tst.login=?"
authorities-by-username-query="
select login,'ROLE_SAVE' from test tst where tst.login=?"
/>
</authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)
在我的RestController上,我添加以下注释:@PreAuthorize(“ isAuthenticated()”)
@RestController
@PreAuthorize("isAuthenticated()")
@RequestMapping("/api/test")
public class PrinterController{
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseStatus test() {
System.out.println("test");
}
Run Code Online (Sandbox Code Playgroud)
但无法正常工作,任何用户都可以使用此资源。
我有一个控制器,并且正在从该控制器返回用户信息:
@RequestMapping(method = RequestMethod.GET)
Object getUserInfo() {
return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
Run Code Online (Sandbox Code Playgroud)
我使用以下方法创建了基于自定义令牌的身份验证OncePerRequestFilter:
package gbyf;
import gbyf.token.Token;
import gbyf.token.TokenRepository;
import gbyf.user.User;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class TokenAuthenticationFilter extends OncePerRequestFilter {
private final TokenRepository tokenRepository;
TokenAuthenticationFilter(TokenRepository tokenRepository) {
this.tokenRepository = tokenRepository;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
String tokenString = request.getHeader("token");
if(tokenString == null) {
// user is …Run Code Online (Sandbox Code Playgroud) 我创建了一个提供用户身份验证的API,它的登录操作由Spring Security 在默认的'/ login'路径上处理.
我想将此路径更改为"api/v1/login".
这是我的安全配置:
http.cors().and().csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/h2-console/**/**").permitAll()
.antMatchers(HttpMethod.POST,"/user/register").permitAll()
.antMatchers("/user/activate").permitAll()
.antMatchers("/user/reset-password").permitAll()
.antMatchers("/user/reset-password").permitAll()
.antMatchers("/admin/user").hasRole("ADMIN")
.antMatchers("/roles").permitAll()
.antMatchers("/user/**").hasRole("USER")
.and()
.formLogin().loginProcessingUrl("/api/v1/login")
.and()
.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
.and()
.addFilterBefore(new ExceptionHandlerFilter(), UsernamePasswordAuthenticationFilter.class)
.addFilter(new JwtAuthenticationFilter(authenticationManager()))
.addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtUserDetailService));
Run Code Online (Sandbox Code Playgroud)
我添加了这一行来改变它:
.formLogin().loginProcessingUrl("/api/v1/login")
Run Code Online (Sandbox Code Playgroud)
但它仍然在'/ login'路径下工作.
"/ api/v1/login"返回404.
有没有办法改变它?
Spring Boot版本:2.0.0.RELEASE
我使用Spring Security,在Spring Security中是否可以使用电子邮件而不是用户名登录?
我的WebSecurityConfigurerAdapter定义为 http.csrf().csrfTokenRepository(csrfTokenRepository());
我想做一个只激活CSRF过滤的集成测试,但是我遇到了一个问题:
我想以下列方式测试:
this.mockMvc = webAppContextSetup(super.webApplicationContext)
.apply(springSecurity(CSRFFilter.class)) //This is just a showcase of that I pretend
.alwaysDo(print())
.build();
Run Code Online (Sandbox Code Playgroud)
如何有选择地将单个过滤器(如CSRFFilter)应用于我的集成测试而不是通用的SecurityMockMvcConfigurers.springSecurity()?
spring-security ×10
spring ×7
java ×5
spring-boot ×3
spring-mvc ×3
ehcache ×1
json ×1
login ×1
logout ×1
rest ×1
spring-cache ×1
spring-test ×1
thymeleaf ×1
uri ×1