【发布时间】:2020-09-24 04:54:35
【问题描述】:
当我在 spring boot 中使用 jsf 时,访问 jsf bean 没有问题,但是当我添加 spring security 时,当我尝试使用 jsf bean 功能访问页面时,我得到 access denied 403,我只能访问带有网址。 我一直在寻找很多来解决这个问题,但没有任何工作,请如果有人可以帮助我解决这个问题。
这是我的代码:
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");
}
}
sn-p 代码视图
<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>
【问题讨论】:
-
你是从哪里学会在课堂上同时使用
@ManagedBean和@Component的?选择一个和正确的对应范围。 ;正在寻找bthev@Autowired你应该使用v@Component -
在 Spring Security 中如何以及在何处访问 jsf bean
-
或者你不能访问页面本身?有什么错误吗?
-
@Kukeltje 如果我取消“@ManagedBean”,它将不是一个 bean,也无法返回视图。如果我选择“@Autowired”,我就不能正确使用注入。如果 spring 安全配置,我想添加对 jsf bean 的访问,但我不知道如何?
-
离题。无论我的“未回答”中的内容如何,您的架构实际上都很好。很多“初学者”做这个waaaaaay更错误。赞美。你这样做就像stackoverflow.com/questions/30639785/…
标签: spring spring-boot jsf spring-security