[使用Spring Security时无法访问jsf bean

问题描述 投票:0回答:1

[当我在Spring Boot中使用jsf时,访问jsf bean没问题,但是当我添加spring security时,当尝试使用jsf bean函数访问页面时,访问被拒绝403,我只能访问带有url的页面。我一直在寻找很多方法来解决此问题,但是没有任何效果,请有人帮助我解决此问题。

这是我的代码:

jsf BeanProduit.java

@ManagedBean
@Component
@SessionScoped
public class BeanProduit {

    @Autowired
    @Qualifier("produitsPrixServiceImpl")
    private CrudService<ProduitsPrix> produitsPrixService;

    @Autowired
    @Qualifier("produitsStockServiceImpl")
    private CrudService<ProduitsStock> produitsStockService;

    private List<ProduitsStock> produits;
    private Logger logger = Logger.getLogger(getClass().getName());

    public BeanProduit() {
        produits = new ArrayList<ProduitsStock>();
    }

    @PostConstruct
    public void init() {
        produits = getListProductsFinal();
    }

    public String loadProduct(int codePdt) {
        logger.info("loading product: " + codePdt);
        try {
            // get product from database
            ProduitsPrix product = produitsPrixService.findById(codePdt);
            // put in the request attribute ... so we can use it on the form page
            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
            Map<String, Object> requestMap = externalContext.getRequestMap();
            requestMap.put("product", product);
        } catch (Exception exc) {
            // send this to server logs
            logger.log(Level.SEVERE, "Error loading product id:" + codePdt, exc);
            // add error message for JSF page
            addErrorMessage(exc);
            return null;
        }
        return "/pages/form-validation";
    }

}

spring security DemoSecurityConfig.java的配置文件

@EnableWebSecurity
public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource securityDataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(securityDataSource);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/assets/**")
            .permitAll()
            .antMatchers("/authentication/login.xhtml?logout").hasAnyRole("EMPLOYEE")
            .antMatchers("/**").hasRole("ADMIN")
            .and().formLogin().loginPage("/authentication/login.xhtml")
            .loginProcessingUrl("/authenticateTheUser").permitAll()
            .defaultSuccessUrl("/", true)
            .and().logout().permitAll()
            .and().exceptionHandling().accessDeniedPage("/error/error-403.xhtml");
    }

}

视图中的代码段

<h:form>
<ui:repeat value="#{beanProduit.produits}" var="produit">
    <tr>
        <td>#{produit.codePdt}</td>
        <td>#{produit.nomPdt}</td>
        <td>#{produit.prixPdt}</td>
        <td>#{produit.qtePdt}</td>
        <td class="text-center">
            <h:commandButton class="btn btn-primary" value="Acheter" action="#{beanProduit.loadProduct(produit.codePdt)}" />
        </td>
    </tr>
</ui:repeat>
</h:form>
spring spring-boot jsf spring-security
1个回答
3
投票

让我开始谈谈您来自以上2020-06-05 13:41:36Z的所有评论:


0
投票

我缺少以下行:

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
    
© www.soinside.com 2019 - 2024. All rights reserved.